--通过insert select 插入数据到已存在的表中
create table grade1
(
id int ,
name varchar(50)
)
insert into grade1 select * from grade
select * from grade1
--通过select into 语句将现有表中的数据添加到新表中,执行两边的话(数据库中已存在名为 'Adress' 的对象。
select gradeid,gradename into Adress from grade
select * from Adress
--插入多行数据,通过union进行插入数据
--union all一次插入多行数据
--union 一次只插入一条数据
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'
--一行受影响,因为值是重复的,union all直接进行插入,不管你是不是重复的
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
--默认值的插入方法,邮箱、手机号、性别都是默认值
--注意,前面没有列名的话所对应的默认值必须要加default,否则会报错的
select * from student
insert into student values('12452','123456','于谦',1,default,'山东济南','1996-06-09',default,26)
--下面的这句话就可以
insert into student(studentno,loginpwd,studentname,gradeid,phone,address,borndate,email,age) values('124521','123456','于谦',1,default,'山东济南','1996-06-09',default,26)
--修改数据
update grade set gradename = 'hs' where gradeid = 12
--无法更新标识列
update grade set gradeid = 20, gradename = 'hs' where gradeid = 12
--删除数据
select * from student
--删除不了,DELETE 语句与 REFERENCE 约束"FK_result_student"冲突。该冲突发生于数据库"myschool",表"dbo.result", column 'studentno'。因为成绩表里面有该学生的成绩
delete from student where studentno = '3'
select * from result
--先删除字表的数据
delete from result where studentno = '3'
--使用truncate删除表中的数据,注意标识列会重新开始
--只能全表删除。这个不是简单的删除记录,这个命令叫删减表,内部机制与delete是不一样的。
--truncate table student
select * from grade
select * from student
sql server案例总结
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SQL Server 账号管理1
SQL Server 账号管理主要包含登录名、用户、架构、角色等管理。通过对账号的管理可以有效的提高数据库系统的安全性,规范运维及使用。注意:登录名是实例下的安全对象,用户、架构及角色是数据库下的安全对象
登录名 用户名 数据库级的主体 SQL Server 级的主体 安全对象 -
SQL Server一致性错误修复案例总结
今天遇到了一个关于数据库一致性错误的案例。海外工厂的`
存储过程 sql sed 元数据 -
SQL Server经典案例问答~!
SQL Server经典案例问答~! 1.要求在yuangong表上增加一列为录入
职场 SQL 数据库 案例 休闲