1. 程式人生 > >sqlserver資料庫 學生表,課程表,成績表,建立語句。

sqlserver資料庫 學生表,課程表,成績表,建立語句。

為了方便練習相應語句,寫下此博文,用於快速建立一個簡單的資料庫。

 

create table Student
(
    Sno char(10)primary key,
    Sname char(10) unique,
    Ssex char(2) check (Ssex in ('男','女')), 
    Sage smallint check(Sage between 18 and 20),
    Sdept char(20),
);
create table Course(
	Cno char(4) primary key, 
	Cname char(20) not null,  
	Cpno char(4),
	Ccredit smallint,
	foreign key (Cpno) references Course(Cno), 
);
create table SC(
	Sno char(10),  
	Cno char(4),
	Grade smallint,
	primary key(Sno,Cno), 
	foreign key(Sno) references Student(Sno),
	foreign key(Cno) references Course(Cno)
);
insert into dbo.Student(Sno, Sname, Ssex, Sage, Sdept)
values('60001','zhangsan','女',18,'art'),
	  ('60002','lisi','女',18,'it'),
	  ('60003','wangwu','女',18,'art'),
	  ('60004','chenliu','女',18,'pe'),
	  ('60005','tisi','女',18,'pe');
	  
INSERT INTO [Course]([Cno],[Cname],[Cpno],[Ccredit])
     VALUES('1000','c#','1002',100),
			('1001','asp.net','1000',100),
			('1002','c',null,100),
			('1003','HTML',null,100),
			('1004','python',null,100),
			('1005','django','1004',100)
     
GO


INSERT INTO [SC]([Sno],[Cno],[Grade])
     VALUES('60001','1000','48'),
			('60002','1003','98'),
			('60001','1001','56'),
			('60001','1004','83'),
			('60001','1003','35'),
			('60002','1002','71'),
			('60003','1005','49'),
			('60005','1002','37')
GO