First, create a database
Create a database called the Student_Mis
. 1 Create Database Student_Mis / * Database name * / 2 ON Primary . 3 (name = ' STUDENT_DATA ' , / * logical file name * / . 4 filename = ' : F. \ The SQL List \ Student_Data_Mis.mdf ' databases in, / * OS store the file name * / 5 size = 10MB, / * database initial storage space * / 6 MAXSIZE = 50MB, / * database maximum storage space * / 7 FILEGROWTH = 5MB) / * number of database growth * / 8 log ON 9 (name = ' Student_Log ', 10 filename = 'F:\SQL list\Student_Log_Mis.ldf', 11 size = 5mb, 12 maxsize =20mb, 13 filegrowth = 5mb)
Second, create a data table
Creating Depts, Students, Courese, Reports four tables
1 say create table and Depts 2 (Dno for char ( 5 ) primary key at the , 3 Dname for char ( 20 ) ? T cassare ) + 4 say create table and Students 5 (SnO for char ( 5 ) primary key at the , 6 Sname for char ( 20 ) ? T cassare , 7 Ssex for char ( 2 ), 8 Birthday Date, 9 Dno char(5), 10 constraint FK_Dno foreign key(Dno) references Depts) 11 12 create table Courses 13 (Cno char(6) primary key, 14 Cname char(20), 15 Pre_Cno char(6), 16 Credits int) 17 create table Reports 18 (Sno char(5), 19 Cno char(6), 20 Grade int check(Grade>=0 and Grade<=100), 21 primary key(Sno,Cno), 22 constraint Student_Report foreign key(Sno) references Students, 23 constraint Courses_Report foreign key(Cno) references Courses)
Third, insert data
1) Insert data Courses
. 1 INSERT INTO Courses 2 values . 3 ( ' C01 ' , ' English ' , null , . 4 ), . 4 ( ' C02 ' , ' data structure ' , ' C05 ' , 2 ), . 5 ( ' C03 ' , ' database ' , ' C02 ' , 2 ), . 6 ( ' C04 ', ' DB_ design ' , ' C03 ' , . 3 ), . 7 ( ' C05 ' , ' C ++ ' , null , . 3 ), . 8 ( ' C06 ' , ' network theory ' , ' C07 ' , . 3 ), . 9 ( ' C07 ' , ' operating system ' , ' C05 ' , . 3 )
2) Insert data Depts
. 1 INSERT INTO Depts 2 values . 3 ( ' D01 ' , ' automatic ' ), . 4 ( ' D02 ' , ' computer ' ), . 5 ( ' D03, ' , ' mathematics ' ), . 6 ( ' D04 ' , ' communication ' ), . 7 ( ' D05 ' , ' electronic ' )
3) Insert data Students
Since the Students table foreign key constraint, so data must be inserted into the Students table in the table Depts
. 1 INSERT INTO Students. 2 values . 3 ( ' S01 ' , ' Jianping ' , ' M ' , ' 1995-10-12 ' , ' D01 ' ), . 4 ( ' S02 is ' , ' Liu ' , ' F ' , ' 1997 -08-21 ' , ' D01 ' ), . 5 ( ' S03 ','Fanlin Jun ' , ' F ' , ' 1998-02-11 ' , ' D02 ' ), . 6 ( ' S04 ' , ' Wei ' , ' M ' , ' 1996-12-22 ' , ' D03, ' ), . 7 ( ' S05 ' , ' Yellow ' , ' male ' , ' 1999-10-31 ' ,' The D03 '), 8 ( ' S06 ' , ' Yangtze ' , ' male ' , ' 1994-04-08 ' , ' D03 ' )
4) Insert data Reports
Because Reports table has table-level integrity constraints, the table of data to be inserted last Reports
1 insert into Reports 2 values 3 ('S01','C01',92), 4 ('S01','C03',84), 5 ('S02','C01',90), 6 ('S02','C02',94), 7 ('S02','C03',82), 8 ('S03','C01',72), 9 ('S03','C02',90), 10 ('S04','C03',75)
The above operation is completed and the database created Student_Mis Courses, Depts, Reports, Students four table creation and data insertion
Reference: Principles of Database and Application Guide (Fourth Edition Science Press)