1. 程式人生 > >SQL創建數據庫、建表、填入內容

SQL創建數據庫、建表、填入內容

gre sql spa table 內容 ble format reference form

--創建數據庫
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)

創建好數據庫,建表,填入內容後準備開始練習

SQL創建數據庫、建表、填入內容