
此题应该是建表和插入数据的经典题目
1. 先建立表(Sno代表学号,sname代表姓名,ssex代表性别,sage代表年龄,sdept代表所在系,cno代表课号,cname代表课程名称,cpno代表先修课号,ccredit代表学分,grade代表成绩)
create table student( sno char(5),
sname varchar(10) not null,
ssex char(2),
sage smallint constraint DF_student_sage default(20),
sdept varchar(20),
constraint PK_student_sno primary key(sno),
constraint CK_student_sage check(sage>0) )
create table course
( cno char(2),
cname varchar(20) not null constraint UQ_course_cname unique,
cpno char(2),
ccredit smallint constraint DF_course_ccredit default(2),
constraint PK_course_cno primary key(cno),
constraint CK_course_ccredit check(ccredit>0),
constraint FK_course_cpno foreign key(cpno) references course(cno) )
create table sc
( sno char(5),
cno char(2),
grade int,
constraint PK_sc_sno_cno primary key(sno, cno),
constraint FK_sc_sno foreign key(sno) references student(sno),
constraint FK_sc_cno foreign key(cno) references course(cno),
constraint CK_sc_cno check(grade>0) )
2. 将记录插入到表中
insert into student(sno, sname, ssex, sage, sdept) values('95001', '李勇', '男', 20, 'CS')insert into student(sno, sname, ssex, sage, sdept) values('95002', '刘晨', '女', 19, 'IS')
insert into student(sno, sname, ssex, sage, sdept) values('95003', '王敏', '女', 18, 'MA')
只要会建表语句和插入语句即可
CREATE TABLE <表名>( <列名> <数据类型> [not null] [[constraint 约束名] default (缺省值)] [[constraint 约束名] unique]
[,其他列的定义]…
[,[constraint 约束名] primary key(列名[, 列名] …)]
[,[constraint 约束名] foreign key(列名[, 列名] …) references 表名(列名[,列名] …)]
[,[constraint 约束名] check(条件)] )
INSERT [INTO] <表名>[(<列名> [,<列名>…] ) ] VALUES(<表达式> [,<表达式>…] )
---1)创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话
CREATE
TABLE
student
(
[id]
[int]
IDENTITY(1,1)
NOT
NULL,
[student_id]
[nvarchar](50)
NULL,
[studen_name]
[nvarchar](50)
NULL,
[age]
[int]
NULL
,
[sex]
[nvarchar](5)
NULL,
[address]
[nvarchar](200)
NULL,
[tel]
[nvarchar](20)
NULL
)
--2)
修改学生表的结构,添加一列信息,学历
education
alter
table
student
add
education
nvarchar(10)
NULL
--3)
修改学生表的结构,删除一列信息,家庭住址
alter
table
student
drop
column
address
--5)
修改学生表的数据,将电话号码以11开头的学员的学历改为“大专”
update
student
set
education='大专'
where
tel
like
'11%'
--6)
删除学生表的数据,姓名以C开头,性别为‘男’的记录删除
delete
student
where
studen_name
like
'C%'
and
sex='男'
--7)
查询学生表的数据,将所有年龄小于22岁的,学历为“大专”的,学生的姓名和学号示出来
select
studen_name,student_id
from
student
where
age<12
and
education='大专'
--8)
查询学生表的数据,查询所有信息,列出前25%的记录
select
TOP
25
PERCENT
*
from
student
--9)
查询出所有学生的姓名,性别,年龄降序排列
select
studen_name,sex,age
from
studen
order
by
age
desc
--10)
按照性别分组查询所有的平均年龄
select
avg(age)
as
age
from
studen
group
by
sex
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)