基本的sql语句

  1. 创建数据库:CREATE DATABASE database-name
  2. 删除数据库:drop database dbname
  3. 创建表:create table tabname(字段属性)
  4. 删除表:drop table tabname
  5. 增加列:Alter table tabname add column col type
  6. 添加/删除主键:Alter table taname add/drop(删除) primary key(列名)
  7. 创建索引:create [unique] index idxname on tabname(col….)
  8. 创建视图:create view viewname as select statement
  9. 查询:select * from table1 where 范围
  10. 插入:insert into table1(field1,field2) values(value1,value2)
  11. 删除:delete from table where 范围
  12.  更新:update table1 set field1=value1 where 范围
  13.  模糊查找:select * from table1 where field1 like ’%value%’
  14. 排序:select * from table order by field1,field2 [desc]
  15. 总数统计:select count(*) from table
  16.  求和:select sum(field) as sumvalue from table
  17. 平均:select avg(field) as avgvalue from table
  18. 最大最小:select max/min(field) as value from table
  19.  查询范围值: select * from table1 where time between time1 and time2
  20. UNION 运算符:请转表的关联查询
  21. EXCEPT 运算符:请转表的关联查询
  22. INTERSECT 运算符:请转表的关联查询
  23. 外连接:请转表的关联查询
  24. 分组:select age,SUM(age) from BaseTable GROUP BY age
  25. 复制表(只复制表结构): select * into b from a where 1<>1
  26. 复制数据: 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
View Code

相关文章:

  • 2022-12-23
  • 2021-10-22
  • 2021-07-29
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
猜你喜欢
  • 2021-05-16
  • 2022-12-23
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2021-09-12
  • 2022-12-23
相关资源
相似解决方案