性别sql
❶ sql 性别 约束
1
创建sm数据库中的选课表sc,包含的列为学号、课程编号、成绩。
create
table
sc(
学号
varchar(10)
not
null,
--数据类型自己定
课程编号
varchar(10)
not
null,
成绩
decimal(3,
0)
)
2
用sql语句给表student的“学号”列添加主键约束。
alter
table
student
add
constraint
pkstudent
primary
key
(学号)
3
用sql语句给表student的“性别”列添加check约束。
alter
table
student
add
constraint
chkstudentsex
check
(
性别
=
'女'
or
性别
=
'男')
4
用sql语句给表course的“课程编号”
列添加主键约束。
alter
table
course
add
constraint
pkcourse
primary
key
(课程编号)
5
用sql语句给表course的“课程名称”
列添加unique约束。
alter
table
course
add
constraint
ukcourse
unique
(课程编号)
6
用sql语句给表sc的“学号”列添加外键约束,参照对象是表student的“学号”列。
alter
table
sc
add
constraint
fkscxh
foreign
key
(学号)
references
student
(学号)
7用sql语句给表sc的“课程编号”列添加外键约束,参照对象是表course的“课程编号”列。
alter
table
sc
add
constraint
fksckc
foreign
key
(课程编号)
references
course
(课程编号)
8用sql语句给表sc的“学号”“课程编号”列添加组合主键约束。
alter
table
sc
add
constraint
pksc
primary
key
(学号,课程编号)
❷ SQL查询,按性别显示学生信息,并分别汇总男生女生人数
如果性别在表Student中的列名称为“sex”的话,那么查询语句分别是:
按性别显示学生信息
Select * from Student order by sex
汇总学生人数
select sex,count(*) as cnt from student group by sex