使用T_SQL創建數據庫 TestSchool
創建一個學生表 TblStudent
創建學生成績表 TblScore q tScoreId (成績 id, 主鍵 , 自動編號)、 tSId (學生編號)、 tEnglish (英語成績)、 tMath (數學成績)
創建老師表 TblTeacher q tTId 、 tTName 、 tTGender 、 tTAge 、 tTSalary 、 tTBirthday
並使用T_sql語句向其中一次填入一條數據或一次填入多條數據的方式填入數據。
(
1)Insert into 表(列) select 列 1 ,列 2 union
2)Insert into 表(列) select 列 1 ,列 2 from 表
3) Select 列 into 新表名 from 舊表
)
復制代碼 代碼如下:
create database TestSchool
on primary
(
name='TestSchool',
filename='F:\SQL Server\TestSchool.mdf',
size=10mb,
filegrowth=10,
maxsize=100mb
)
log on
(
name='TestSchool_log',
filename='F:\SQL Server\TestSchool_log.ldf'
)
create table TblStudent
(
studentId int identity(1,1) primary key,
tScoreId int not null,
sName nvarchar(50) not null,
sAge int not null,
sNo numeric(18,0),--身份證號,十八位數字,小數位0
sEmail varchar(50),
sGender bit default(1),
sBirthday datetime
)
select * from TblStudent
truncate table TblStudent
insert into TblStudent
select 1,'劉備',20,123456789012345678,'[email protected]',1,'1987-5-6' union
select 1,'關羽',19,123456789012345671,'[email protected]',1,'1988-8-6' union
select 1,'張飛',18,123456789012345672,'[email protected]',1,'1989-5-19' union
select 4,'曹操',22,123456789012345673,'[email protected]',1,'1985-12-6' union
select 4,'夏侯惇',22,123456789012345674,'[email protected]',1,'1985-3-6' union
select 4,'華佗',50,12345678901234565,'[email protected]',1,'1957-1-16' union
select 4,'甄姬',18,12345678901234565,'[email protected]',0,'1989-8-8'
create table TblScore
(
tScoreId int identity(1,1) primary key,
studentId int not null, --學生id,外鍵
tEnglish float,
tMath float
)
select * from TblScore
truncate table TblScore
insert into TblScore
select 1,90,97 union
select 2,90,70 union
select 3,59,100 union
select 4,100,80 union
select 5,60,96 union
select 6,0,100 union
select 7,80,60
create table TblTeacher
(
tTId int identity(1,1) primary key,
tTName nvarchar(50) not null,
tTGender bit default(1),
tTAge int,
tTSalary money,
tTBirthday datetime
)
select * from TblTeacher
insert into TblTeacher
select '商炳奇',1,22,10000,'1991-10-30' union
select '劉祎',0,22,10000,'1991-11-06' union
select '耿宇丹',0,21,10000,'1992-12-30' union
select '張少丹',0,22,10000,'1991-6-6' union
select '王靜靜',0,22,10000,'1991-6-6' union
select '段琳琳',0,22,10000,'1991-6-6' union
select '楊巧巧',0,21,10000,'1991-6-6'