10.23 SQL 复习

来源:互联网 发布:淘宝网的经营模式 编辑:程序博客网 时间:2024/06/10 19:19

设置文件夹权限
生成脚本
数据库备份与还原恢复

create database 库名
on primary
(


)
log on
(

)


create   table
(id int identity(1,1) primary key,
gender bit default(1),
birthday datatime


)

数据更新
update 表名 set name='' where name=''
create table Class
(

clsId int identity(1,1) primary key,
clsName nvarchar(50) not null,
clsDscr nvarchar(1000)
)

先把表删除再改

创建学生表
create table Student

sClassId int,
sName nvarchar(20),
sAge int,
sSex nchar(2),
sNumber varchar(50) ,
sBirthday  datetime

给上表添加数据

学生表加学生Id(update语句)
-----------------------------

把tblTeacher 表中年龄为19 岁的姓名两边加上五角星并把性别都设为女,工资都加上500
update tblTeacher set tTName='★'+tTName+'★',tTGebder=0,tTSalary=tTSalary+500 where tTAge=19

这里可以使用+=:tTSalary+=500(但只有SQL2008 的可以用)
----------
将年龄为19 并且性别为女的人的姓名两边再加上五角星
update tblTeacher set tTName='☆'+tTName+'☆' where tTAge=19 and tTGender=0


把name中的★ 替换为☆
update tblTeacher set tTName=replace(tTName,'★','☆')
where tTAge=19 and tTGender=0

T_Sql 是mssqlserver中

----------逻辑运算符的优先级问题 not and or

-------删除
delect  from tblTeacher where tTAge=19 or tTAge is null

insert into tblTeacher values('张三',1,20,10000,'2000-10-10')

/*改自动增长的编号
set identity_insert tblTeacher on


*/
--用truncate来删除,默认值就会回到初始种子值1
truncate table tblTeacher ;删除表
insert into tblTeacher values('李四',1,20,10000,'2000-10-10')

delete from 表名 truncate table 表名
区别:
1.delete  不能使自动编号返回为起始值 但是truncate能是自动增长的列的值返回为默认的种子
2.truncate  只能一次清空,不能按条件删除。但是delete 可以按条件删除 部分记录
3.truncate清空数据表性能(速度)比Delete  快很多
4.truncate不会记录到系统日志,不会触发delete 触发器


---------创建成绩表
PPT第二题

考试题难,所有人成绩加5 分
update Score set english=english+5,math=math+5
分情况对待
update Score set english=english+5 where english+5<=100

update Score set math=math+5 where math+5<=100

update Score set english=100 where english+5>=100

update Score set math100 where math+5>=100

case 也可以分情况来做
update Score
set english=
(
case when english+5<=100 then english+5
when english+5>=100 then 100
end
),
math=
(
case when math+5<=100 then math+5
when math+5>=100 then 100
end
)
------------------------
约束(保证数据的完整性)

create database Test
----建员工表
create table Employee
(
EmpId  int  identity (1,1) primary key,
EmpName  nvarchar(20) not null,
EmpGender nchar(2),
EmpAge int,
EmpEmail  varchar(20),
EmpAddress  nvarchar(50)
)
go
----建部门表
create table Department
(
DepId int indentity(1,1) primary key,
DepName nvarchar(20)
)


约束  
check  约束
EmpGender='男' or EmpGender= '女'

原创粉丝点击