sql server案例总结
生活随笔
收集整理的這篇文章主要介紹了
sql server案例总结
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
--通過(guò)insert select 插入數(shù)據(jù)到已存在的表中
create table grade1
(
id int ,
name varchar(50)
)insert into grade1 select * from grade select * from grade1--通過(guò)select into 語(yǔ)句將現(xiàn)有表中的數(shù)據(jù)添加到新表中,執(zhí)行兩邊的話(數(shù)據(jù)庫(kù)中已存在名為 'Adress' 的對(duì)象。
select gradeid,gradename into Adress from grade select * from Adress--插入多行數(shù)據(jù),通過(guò)union進(jìn)行插入數(shù)據(jù)
--union all一次插入多行數(shù)據(jù)
--union 一次只插入一條數(shù)據(jù)
insert into grade (gradename)
select 'Y2'union all
select 'Y2'union all
select 'Y2'union all
select 'Y2'union all
select 'Y2'-------5行受影響
insert into grade (gradename)
select 'Y21'union
select 'Y22'union
select 'Y23'union
select 'Y24'union
select 'Y25'--一行受影響,因?yàn)橹凳侵貜?fù)的,union all直接進(jìn)行插入,不管你是不是重復(fù)的
insert into grade (gradename)
select 'Y2'union all
select 'Y2'union all
select 'Y2'union all
select 'Y2'union all
select 'Y2'select * from grade--默認(rèn)值的插入方法,郵箱、手機(jī)號(hào)、性別都是默認(rèn)值
--注意,前面沒(méi)有列名的話所對(duì)應(yīng)的默認(rèn)值必須要加default,否則會(huì)報(bào)錯(cuò)的
select * from student
insert into student values('12452','123456','于謙',1,default,'山東濟(jì)南','1996-06-09',default,26)
--下面的這句話就可以
insert into student(studentno,loginpwd,studentname,gradeid,phone,address,borndate,email,age) values('124521','123456','于謙',1,default,'山東濟(jì)南','1996-06-09',default,26)--修改數(shù)據(jù)
update grade set gradename = 'hs' where gradeid = 12
--無(wú)法更新標(biāo)識(shí)列
update grade set gradeid = 20, gradename = 'hs' where gradeid = 12--刪除數(shù)據(jù)
select * from student
--刪除不了,DELETE 語(yǔ)句與 REFERENCE 約束"FK_result_student"沖突。該沖突發(fā)生于數(shù)據(jù)庫(kù)"myschool",表"dbo.result", column 'studentno'。因?yàn)槌煽?jī)表里面有該學(xué)生的成績(jī)
delete from student where studentno = '3'
select * from result
--先刪除字表的數(shù)據(jù)
delete from result where studentno = '3'--使用truncate刪除表中的數(shù)據(jù),注意標(biāo)識(shí)列會(huì)重新開(kāi)始
--只能全表刪除。這個(gè)不是簡(jiǎn)單的刪除記錄,這個(gè)命令叫刪減表,內(nèi)部機(jī)制與delete是不一樣的。
--truncate table studentselect * from gradeselect * from student
總結(jié)
以上是生活随笔為你收集整理的sql server案例总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 猫鼠游戏剧情解析 猫鼠游戏剧情介绍
- 下一篇: 欧洲重卡配置要求?