MSSql Server基础学习系列———数据添加

来源:互联网 发布:国学什么软件最好 编辑:程序博客网 时间:2024/06/05 17:12

上一篇文章讲解了如果进行数据库数据的检索,今天就接着讲如何进行数据库数据的添加

--不建议使用--如果后期数据库的表结构发生改变,会因为没未改字段赋值而报错--插入日期字段的时候 要把时间当成一个字符串insert into dbo.Coursevalues('111','110')--推荐使用insert into dbo.Course(cname,cperiod)values('111','110')--部分列插值insert into dbo.Course(Cname)values('110')--insert 语句对于自动增长的列不需要赋值,可能自行增长(插入失败的时候也会自动增长)--如果需要手动添加自动增长字段,则需要首先使用下面的语句打开手动修改自动增长字段操作set identity_insert course oninsert into Course(Cname, Cperiod)values('111','110')--关闭手动修改自动增长字段操作set identity_insert course off--批量插入数据--insert into后面可以跟结果集insert into dbo.Course(Cname)select '数据结构'--使用unioninsert into dbo.Course(Cname)select 'C语言'union select 'C++'union select 'Java'--批量插入表中查询出来的数据insert into [TestDB].[dbo].[user](Uname)select Cnamefrom dbo.Course--要求插入的字段数量相同以及对应的数据类型能够兼容--表中数据的复制(附带着创建了一个新表可用来进行数据的备份)--全部数据复制select * into NewCourse from dbo.Course--部分数据复制select Cname,Cperiod into NewCourse1 from dbo.Course--条件复制select Cname into newCourse4 from dbo.Course where Cid<10--创建一个与旧表字段相同的新表,但是该新表不存在条件约束select * into NewCourse3 from dbo.Course where 0>1

0 0
原创粉丝点击