sqlserver中的标识列(标识列又称标识符列,习惯上又叫自增列)

/*
标识列必须是int类型的
标识列不可以编辑
标识列先自增,再验证,最后插入数据
*/
drop table employee
create table employee(
empId int identity(6,2) primary key, --如果种子和自增量都是1的话,可以省略不写
empName varchar(30),
empAge int,
empAddress varchar(30)
--[name] varchar(20)
/*
字段名和表名起名字的时候最好不要起有可能是关键字的名字,如
果怕是关键字或者你非要起这种敏感的名字最好是用[]括起来
*/
)
select * from employee
/*into关键字可以省略,但是最好不要省略,为
了规范,每行写5个字段,这样比较好对齐和匹配
*/
insert into employee
(empName,empAge,empAddress)
values
('李四',21,'北门')
select * from student
insert into student
(stuId,stuName,stuAge,stuSex,stuTel,stuAddress)
values
(9,'王五',28,0,'1378888888',default)
/*
select * into 新表 from 旧表 (相当于把表备份了,只备份
表结构与数据,约束消失)
*/
select * into student2 from student
select * from student2
----select 列名 into 新表 from 旧表 (相当于把表备份了,只需要其中几列)
select stuName,stuSex,stuAddress into student3 from student
select * from student3
----select 列名 as 别名 into 新表 from 旧表 (相当于把表备份了,只需要其中几列,可用as加别名)
select stuName as '姓名',stuSex as 性别,stuAddress 地址 into student4 from student
select * from student4
 
create table myUsers(
userName varchar(20),
userAddress varchar(30)
)
select * from myUsers
 
/*
insert into 表名 (列名1, 列名2) select 
列名1, 列名2 from 表名
注意3点:
1.列的个数一致
2.对应列的数据类型一致
3.数据类型的长度一致(目标表的长度大于等于源表的长度)
*/
insert into myUsers
	(userName, userAddress)
select 
	 stuName, stuAddress 
from 
	student 
where 
	stuAddress = '地址不详'
 
/*
insert into 表名 (列名1, 列名2)
select 列名1, 列名2 
union 
select 列名3, 列名4
注意:
1.不支持default关键字
2.当两行数据相同时理解为一行(即union关
键字会自动过滤掉重复行,所以只插入一行数据)
3.使用union all关键字才可以插入多行重
复的记录,所以是插入多行数据
*/
 
/*
使用union关键字的话,union关键字会自动过
滤掉重复行,所以只插入一行数据
*/
insert into myUsers 
(userName, userAddress)
select '二狗子', '深圳' union 
select '二狗子', '深圳' union 
select '二狗子', '深圳'
 
/*
使用union all关键字才可以插
入多行重复的记录,所以是插入多行数据
*/
insert into myUsers 
(userName, userAddress)
select '二狗子', '江西省赣州市于都县' union all
select '二狗子', '江西省赣州市于都县' union all
select '二狗子', '江西省赣州市于都县'
select * from myUsers
 
insert into myUsers 
(userName, userAddress)
select '令狐冲', '华山' union all
select '杨过', '山谷' union all
select '张无忌', '光明顶' union all
select '韦小宝', 'default'

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值