当前位置:   article > 正文

SQL Server创建数据表(以学生表,教师表,成绩表,课程表为例)_sql建表时成绩

sql建表时成绩

--创建学生表,包括:学号,学生姓名,出生年月,性别
create table [wmj_Student]
(
    [StudentId] bigint not null,    
    [StudentName] nvarchar(100) null,   
    [Birthday] datetime null,
    [Gender] bit not null                 
    constraint [pk_student_id] primary key clustered ([StudentId] asc) 
)
--添加学生数据
insert into [wmj_Student]
    ([StudentId],[StudentName],[Birthday],[Gender])
values
    (0001,'猴子',1989-1-1,1),
    (0002,'猴子',1989-12-21,0),
    (0003,'马云',1991-12-21,1),
    (0004,'王思聪',1990-12-21,1)

--创建课程表,包括:课程号,课程名称,教师号
create table [wmj_Course]
(
    [CourseId] bigint primary key not null,    --主键
    [CourseName] nvarchar(100) null,   
    fk_teacher_id bigint foreign key references [wmj_Teacher](TeacherId) --外键
)
--添加课程表数据
insert into [wmj_Course]
    ([CourseId],[CourseName],fk_teacher_id)
values
    (1001,'语文',0002),
    (1002,'数学',0001),
    (1003,'英语',0003)

--创建成绩表(模拟学生表和课程表的关系),包括:学生学号,课程号,成绩
create table [wmj_Score]
(
    [StudentId] bigint constraint fk_student_id foreign key references [wmj_Student](StudentId),    
    [CourseId] bigint foreign key references [wmj_Course](CourseId),   
    [Score] int null              
    constraint pk_student_id_score_id primary key(StudentId,CourseId) --将学号和课程号作为主键
)
--导入excel数据

--创建教师表,包括:教师号,教师姓名
create table [wmj_Teacher]
(
    [TeacherId] bigint not null, 
    [TeacherName] nvarchar(100) null            
    constraint [pk_teacher_id] primary key clustered ([TeacherId] asc) 
)
--添加老师数据
insert into [wmj_Teacher]
    ([TeacherId],[TeacherName])
values
    (0001,'孟扎扎'),
    (0002,'马化腾'),
    (0003,null)
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/489646
推荐阅读
相关标签
  

闽ICP备14008679号