数据库练习
2012-10-23 21:46
330 查看
插入 几条老师信息 和成绩、用SQL插入几条员工信息(注意:bit类型,在写代码中用1或0来表示,不要用’false’,会进行类型转换的。)
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
)
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'
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
)
insert into TblStudent
select 1,'刘备',20,123456789012345678,'123@163.com',1,'1987-5-6' union
select 1,'关羽',19,123456789012345671,'1sfdsf3@163.com',1,'1988-8-6' union
select 1,'张飞',18,123456789012345672,'12sfsd3@163.com',1,'1989-5-19' union
select 4,'曹操',22,123456789012345673,'12sf3@163.com',1,'1985-12-6' union
select 4,'夏侯惇',22,123456789012345674,'1ff23@163.com',1,'1985-3-6' union
select 4,'华佗',50,12345678901234565,'12ff3@163.com',1,'1957-1-16' union
select 4,'甄姬',18,12345678901234565,'12f3@163.com',0,'1989-8-8'
create table TblScore
(
tScoreId int identity(1,1) primary key,
studentId int not null, --学生id,外键
tEnglish float,
tMath float
)
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
练习1:给studentId是1的英语成绩加10分
update TblScore set tEnglish=tEnglish+10 where studentId=1
练习2:考试题偏难,所有人的成绩加5分
update TblScore
set tEnglish=
(
case when tEnglish+5<=100 then tEnglish+5
when tEnglish+5>=100 then 100
end
),
tMath=
(
case when tMath+5<=100 then tMath+5
when tmath+5>=100 then 100
end
)
练习3:所有女学生的年龄减1岁
update TblStudent set sAge=sAge-1 where sGender=0
删除工资大于2000的老师
delete from TblTeacher where tTSalary>2000
============将老师表清空========
删除所有老师
delete from TblTeacher
删除数据时候 把自增长列的值还原成种子
truncate table TblTeacher
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
)
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'
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
)
insert into TblStudent
select 1,'刘备',20,123456789012345678,'123@163.com',1,'1987-5-6' union
select 1,'关羽',19,123456789012345671,'1sfdsf3@163.com',1,'1988-8-6' union
select 1,'张飞',18,123456789012345672,'12sfsd3@163.com',1,'1989-5-19' union
select 4,'曹操',22,123456789012345673,'12sf3@163.com',1,'1985-12-6' union
select 4,'夏侯惇',22,123456789012345674,'1ff23@163.com',1,'1985-3-6' union
select 4,'华佗',50,12345678901234565,'12ff3@163.com',1,'1957-1-16' union
select 4,'甄姬',18,12345678901234565,'12f3@163.com',0,'1989-8-8'
create table TblScore
(
tScoreId int identity(1,1) primary key,
studentId int not null, --学生id,外键
tEnglish float,
tMath float
)
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
练习1:给studentId是1的英语成绩加10分
update TblScore set tEnglish=tEnglish+10 where studentId=1
练习2:考试题偏难,所有人的成绩加5分
update TblScore
set tEnglish=
(
case when tEnglish+5<=100 then tEnglish+5
when tEnglish+5>=100 then 100
end
),
tMath=
(
case when tMath+5<=100 then tMath+5
when tmath+5>=100 then 100
end
)
练习3:所有女学生的年龄减1岁
update TblStudent set sAge=sAge-1 where sGender=0
删除工资大于2000的老师
delete from TblTeacher where tTSalary>2000
============将老师表清空========
删除所有老师
delete from TblTeacher
删除数据时候 把自增长列的值还原成种子
truncate table TblTeacher
相关文章推荐
- Ado.Net小练习01(数据库文件导出,导入)
- 数据库操作练习2
- 经典SQL学习笔记 (九)-比赛信息数据库练习
- [代码阅读]数据库源代码阅读练习
- SQL语言基础练习---数据库server 2008(四)
- 数据库语句练习
- 数据库练习
- 8.手工备份恢复--备用数据库(练习10、11)
- 【Intermediate SQL-1 】五道较难的sql练习_大连理工软院数据库上机答案
- 计算机基础--数据库专项练习笔记
- php 数据库练习之租房子
- C#-WebForm-ASP开发练习:从数据库中动态添加信息
- 数据库练习
- 数据库练习表(包含省份,市县)
- Mac OS X 10.5.8下的MySQL 5.0.77动物园数据库教程练习命令
- AJAX自学练习二:无刷新从数据库后台取数据显示
- Ado.Net小练习01(数据库文件导出,导入)
- 数据库练习命令
- 数据库基础语句,聚合函数,练习