SQL创建数据库、建表、填入内容

复制代码
--创建数据库
create database Information

go

--使用数据库
use Information

go

--创建表
create table Student
(
 Sno nvarchar(50) primary key not null,
 Sname nvarchar(50) not null,
 Ssex bit not null,
 Sbirthday datetime,
 Class nvarchar(50),
)

create table Course
(
    Cno nvarchar(50) primary key not null,
    Cname nvarchar(50) not null,
    Tno nvarchar(50) not null,
)

create table Score
(
    Sno nvarchar(50) not null,
    Cno nvarchar(50) not null,
    Degree decimal(4,1),
)

create table Teacher
(
    Tno nvarchar(50) primary key not null,
    Tname nvarchar(50) not null,
    Tsex bit not null,
    Tbirthday datetime,
    Prof nvarchar(50),
    Depart nvarchar(50) not null,
)


--填入数据  Student
insert into Student values('108','曾华','1','1977-09-01','95033')
insert into Student values('105','匡明','1','1975-10-02','95031')
insert into Student values('107','王丽','0','1976-01-23','95033')
insert into Student values('101','李军','1','1976-02-20','95033')
insert into Student values('109','王芳','0','1975-02-10','95031')
insert into Student values('103','陆君','1','1974-06-03','95031')


--填入数据  Course
insert into Course values('3-105','计算机导论','825')
insert into Course values('3-245','操作系统','804')
insert into Course values('6-166','数字电路','856')
insert into Course values('9-888','高等数学','831')


--填入数据  Score
insert into Score values('103','3-245','86')
insert into Score values('105','3-245','75')
insert into Score values('109','3-245','68')
insert into Score values('103','3-105','92')
insert into Score values('105','3-105','88')
insert into Score values('109','3-105','76')
insert into Score values('101','3-105','64')
insert into Score values('107','3-105','91')
insert into Score values('108','3-105','78')
insert into Score values('101','6-166','85')
insert into Score values('107','6-166','79')
insert into Score values('108','6-166','81')


--填入数据 Teacher
insert into Teacher values('804','李诚','1','1958-12-02','副教授','计算机系')
insert into Teacher values('856','张旭','1','1969-03-12','讲师','电子工程系')
insert into Teacher values('825','王萍','0','1972-05-05','助教','计算机系')
insert into Teacher values('831','刘冰','0','1977-08-14','助教','电子工程系')



--主外键关系
--如表A中的Ids是主键,要约束表B中的Aid列,那么语句应该是:
--alter table B add constraint A_B_Ids foreign key(Aid)  references A(Ids) 



--Student 中的Sno    约束      Score  中的  Sno
alter table Score add constraint Student_Score_Sno foreign key(Sno) references Student(Sno)


--Course 中的 Cno     约束     Score  中的  Cno
alter table Score add constraint Course_Score_Cno foreign key(Cno) references Course(Cno)


--Teacher 中的 Tno     约束     Course  中的   Tno
alter table Course add constraint Teacher_Course_Tno foreign key(Tno) references Teacher(Tno)
复制代码

创建好数据库,建表,填入内容后准备开始练习

posted @   Jokerpapapa  阅读(721)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示