select 和 insert用法
1、select语法:select column1,column2 from table_name where condition1 or/and condition2
- select * from Information --查询Information表中的信息 * 代表所有列
- select * from Information where mem_major = '物联网工程' --where 后面表示条件
- select * from Information where mem_major = '物联网工程' or mem_major = '电子信息工程' --条件的或,只满足一个就能查询出来,还有与 and ,同时满足才能查询到,
- select * from Information where isnull(mem_major,'') = '' --表示mem_major这一类是否是空字符串,如果是,则视为null,查询的话可以当成null查询出来
- select mem_major,mem_dir,mem_name from Information --查询一些列在Information表中
- select mem_major,mem_dir,mem_name decration into Sim_Info from Information --将查询到的这些列放到新的表Sim_Info中
- select mem_major,mem_dir,mem_name,cast('' as varchar(200) ) decration into Sim_Info from Information --cast('' as varchar(200)) 将''空字符串作为varchar(200)的说明字符
- select object_id('Information') --查询表达id号
- select * from syscolumns where id = object_id('Information') --查询id的表
- select * from Information where mem_grade = 1602 or mem_grade = 1601
- select * from Information where mem_grade in (1601,1602)
- select * from Information where mem_grade not in (1601,1602)
- select * from sysobjects where xtype = 'PK' --查询数据库中哪些表
U 是查询表
P 是查询存储过程
V 是查询视图
T 是查询触发器
F 是查询函数
PK 是查询主键
FK 是查询外键
- select 还有更改的用途
declare @i int
select @i = 22
insert Information select '小明',@i,'男' --直接插入所有列,可用变量
2、insert语法: insert table_name(column1,column2) values(value1,value2)
- insert Information(mem_name,mem_age,mem_sex) values('小明',22,'男')
- insert Information values('...','...') --写入全部列的信息
- insert Information select '小明',22,'男' --直接插入所有列
3、其他一些知识
- 添加特殊约束
alter table Information
add mem_phone nvarchar(11) not null default('') --添加mem_phone 列,类型nvarchar , 非空,默认值''
constraint UQ_Phone unique(mem_phone) --特殊约束,不能重复出现
- 更改一个表中的内容
alter table Information
update Information set mem_major = '物联网工程' where mem_major = '物联网' --更改表中某一列的值,将'物联网'改为'物联网工程',set为设置