SQL Server创建 学号 性别 课程编号 check约束 主键约束 UNIQUE约束

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server创建 学号 性别 课程编号 check约束 主键约束 UNIQUE约束相关的知识,希望对你有一定的参考价值。

实验4 数据控制
实验名称:数据控制
目的要求:
(1) 理解数据完整性的概念;
(2) 掌握约束的定义和使用;
(3) 了解SQL Server的数据完整性规则
实验内容:
1 创建SM数据库中的选课表SC,包含的列为学号、课程编号、成绩。
2 用SQL语句给表Student的“学号”列添加主键约束。
3 用SQL语句给表Student的“性别”列添加check约束。
4 用SQL语句给表Course的“课程编号” 列添加主键约束。
5 用SQL语句给表Course的“课程名称” 列添加UNIQUE约束。
6 用SQL语句给表SC的“学号”列添加外键约束,参照对象是表Student的“学号”列。
7用SQL语句给表SC的“课程编号”列添加外键约束,参照对象是表Course的“课程编号”列。
8用SQL语句给表SC的“学号”“课程编号”列添加组合主键约束。

use SM
go
1.
//创建SC表
if exists(select * from sysobjects where name='sc') //判断数据中是否有该表,若有先删除再重建
drop table sc
create table sc
(
StudentNo varchar(20) not null,
CourseNo varchar(20) not null,
StudentResult int not null
)
go
//添加约束
2.
alter table Student add constraint PK_stuno primary key(StudentNo)
3.
alter table Student add constraint CK_sex check (sex='男' or sex='女')
4.
alter table Course add constraint PK_courseno primary key(CourseNo)
5.
alter table Course add constraint UQ_coursename unique (CourseName)
6.
alter table sc add constraint FK_studentno foreign key(StudentNo) references Student(StudentNo)
7.
alter table sc add constraint FK_courseno foreign key(CourseNo) references Course(CourseNo)
8.
alter table sc add constraint PK_stu primary key(StudentNo,CourseNo)
参考技术A 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 (学号,课程编号)本回答被提问者采纳
参考技术B 1, create table sc(

SubjectId int ,
SubjectNo varchar(10),
Exam double
)

2,alter table student add constraint Pk_Stu primary key clustered (StuNo)

3,alter table student add constraint CK_stu check (stuSex='男' or stuSex='女')

4,alter table Course add constraint Pk_Course primary key clustered (CourNo)

5,alter table Course add constraint UP_c unique(SubjName)

6,alter table sc add constraint PK_s FOREIGN KEY SubjectId
REFERENCES Student stid

7,alter table sc add constraint PK_s FOREIGN KEY SubjectId
REFERENCES Course CouNo

8,alter table sc add constraint PK_SC primary key(SubjectId,SubjectNo)

mysql三表查询sql语句

  1. 表结构:

Student学生表(学号、姓名、性别、年龄、编辑)

Course课程表(编号、课程名称)

sc选课表(选课编号、学号、课程编号、成绩)

(1)写一个SQL语句,查询选修了“计算机原理”的学生学号和姓名

(2)写一个SQL语句,查询“小明”同学选修的课程名称

(3)写一个SQL语句,查询选修了5门课程的学生学号和姓名

 

答案:

1

select student.stu_no,student.stu_name

from student,course,sc

where course.c_no=sc.c_no and sc.stu_no=student.stu_no and course.c_name=‘计算机原理‘;

2

select course.c_name,student.stu_name

from student,course,sc

where student.stu_name=‘小明‘ and student.stu_no=sc.stu_no and sc.c_no=course.c_no;

3

select student.stu_no,stu_name

from student,course,sc

where student.stu_no=sc.stu_no and sc.c_no=course.c_no

group by student.stu_no

having count(*)=5

 

建表及插入数据可参考如下语句:

create table student(

stu_no int,

stu_name varchar(10),

sex char(1),

age int(3),

edit varchar(20)

);

 

insert into student values

(1,‘wang‘,‘‘,21,‘hello‘),

(2,‘小明‘,‘‘,22,‘haha2‘),

(3,‘hu‘,‘‘,23,‘haha3‘),

(4,‘li‘,‘‘,25,‘haha4‘);

 

create table course(

c_no int,

c_name varchar(10)

);

 

insert into course values

(1,‘计算机原理‘),

(2,‘java‘),

(3,‘c‘),

(4,‘php‘),

(5,‘py‘);

 

create table sc(

sc_no int,

stu_no int,

c_no int,

score int(3)

);

 

insert into sc values

(1,1,1,80),

(2,2,2,90),

(3,2,1,85),

(4,2,3,70),

(5,2,4,95),

(6,2,5,89);

以上是关于SQL Server创建 学号 性别 课程编号 check约束 主键约束 UNIQUE约束的主要内容,如果未能解决你的问题,请参考以下文章

SQL的嵌套问题

在SQL server中查询每个学生的班级、学号、姓名、平均分,结果按平均分降序排列,平均分相同者按班级排列

mysql三表查询sql语句

写一个SQL语句,查询选修了5门课程的学生学号和姓名

sql语言多表查询

SQL Server 之T-SQL语言的学习