基本的sql语句
- 创建数据库:CREATE DATABASE database-name
- 删除数据库:drop database dbname
- 创建表:create table tabname(字段属性)
- 删除表:drop table tabname
- 增加列:Alter table tabname add column col type
- 添加/删除主键:Alter table taname add/drop(删除) primary key(列名)
- 创建索引:create [unique] index idxname on tabname(col….)
- 创建视图:create view viewname as select statement
- 查询:select * from table1 where 范围
- 插入:insert into table1(field1,field2) values(value1,value2)
- 删除:delete from table where 范围
- 更新:update table1 set field1=value1 where 范围
- 模糊查找:select * from table1 where field1 like ’%value%’
- 排序:select * from table order by field1,field2 [desc]
- 总数统计:select count(*) from table
- 求和:select sum(field) as sumvalue from table
- 平均:select avg(field) as avgvalue from table
- 最大最小:select max/min(field) as value from table
- 查询范围值: select * from table1 where time between time1 and time2
- UNION 运算符:请转表的关联查询
- EXCEPT 运算符:请转表的关联查询
- INTERSECT 运算符:请转表的关联查询
- 外连接:请转表的关联查询
- 分组:select age,SUM(age) from BaseTable GROUP BY age
- 复制表(只复制表结构): select * into b from a where 1<>1
- 复制数据: insert into b(列名) select 列名 from b;
索引
索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。
索引分为聚簇索引和非聚簇索引两种,聚簇索引 是按照数据存放的物理位置为顺序的,而非聚簇索引就不一样了;聚簇索引能提高多行检索的速度,而非聚簇索引对于单行的检索很快。
触发器
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程。触发器主要是通过事件进行触发被自动调用执行的。触发器对表进行插入、更新、删除的时候会自动执行的特殊存储过程。
触发器分为after触发器,insert触发器, update触发器, delete触发器。
创建触发器语法:
create trigger tgr_name
on table_name
for 触发器类型
AS
数据库执行的语句
触发器实例
在ado数据库创建BaseTable的插入触发器:
在向表BaseTable插入一条记录时,同时触发器会向Table表也插入一条数据作为记录触发器执行成功。
ALTER TRIGGER [dbo].[tgr_name] ON [dbo].[BaseTable] for INSERT AS BEGIN declare @name varchar(20),@id int; select @id = id, @name = name from inserted; set @name = @name + convert(varchar, @id); insert into [Ado].[dbo].[Table] values(@name, 18 + @id); END