增*
----------------------------------------------------创建数据库----------------------------------------------
create database 数据库名
on primary
(
)
log on
(
)
-----------------------------------------------------创建表格------------------------------------------------
create table 表名
(
)
--------------------------------------------------表格中插入数据--------------------------------------------
insert into 表名(列名) values(列数据)
----------------------------------------------------表中插入列-----------------------------------------------
alter table 表名 add 列名 类型(最大范围)
-----------------------------------------------------复制所有------------------------------------------------
select * into 表名 from 表名
-----------------------------------------------------复制列名-----------------------------------------------
select top 0 into 表名 from 表名
-----------------------------------------------------创建视图-----------------------------------------------
create View 视图名 as select * from 表名
********删
----------------------------------------------------删除数局库-----------------------------------------------
drop database 数据库名
---------------------------------------------------删除表中某行----------------------------------------------
delete from 表名 where 条件
---------------------------------------------------删除表中某列----------------------------------------------
alter table 表名 drop column 列名
******改
----------------------------------------------------更改列值------------------------------------------------
update 表名 列名=列值 where 条件
---------------------------------------------修改表中列的数据类型------------------------------------------
alter table 表名 alter colunm 列名 数据类型(最大范围)
查*
--------------------------------------------------查询表格所有数据-------------------------------------------
select * from 表名
--------------------------------------------查询表中某些列的所有行----------------------------------------
select 列名 from 表
--------------------------------------------查询表中某些行的所有列----------------------------------------
select * from 表名 where 条件
---------------------------------------------查询某列并改变列名-------------------------------------------
select 列名 as 新列名 from 表名
---------------------------------------在查询结果中增加一列并赋值---------------------------------------
select *,新列=列值 from 表名
---------------------------------------------------升序排列--------------------------------------------------
select * from 表名 order by 列名
---------------------------------------------------降序排列--------------------------------------------------
select * from 表名 order by 列名 desc
---------------------------------------------------取前几个--------------------------------------------------
select top 数字 * from 表名 order by 列名
--------------------------------------------------取前百分之多少---------------------------------------------
select top 数字 percent * from 表名 order by 列名
------------------------------------------------查询显示某列的和---------------------------------------------
select sum(列名) as 新列名 from 表名
---------------------------------------------------统计行数--------------------------------------------------
select count(*) as 新列名 from 表名
---------------------------------------------------查询平均值------------------------------------------------
select avg(列名*1.0) as 新列名 from 表名
---------------------------------------------------查询最大值------------------------------------------------
select max(列名) as 新列名 from 表名
---------------------------------------------------查询最小值------------------------------------------------
select min(列名) as 新列名 from 表名
---------------------------------------------------模糊查询-------------------------------------------------
—任意单个字符’_’
—任意多个字符’%’
—范围’[]’
—非’^’
select * from 表名 where 列名 like ‘p_x’
---------------------------------------------------空值处理-------------------------------------------------
—查询空
select * from 表名 where 列名 is null
—查询非空
select * from 表名 where 列名 is not null
---------------------------------------------------查询分组-------------------------------------------------
select * from 表名 group by 列名
----------------------------------------------行拼接查询(联合查询)-------------------------------------
select * from 表名 union select * from 表名
—对分组后的表在最后加一行汇总
select 列名 from 表名 group by 列名 union select 列名 from 表名
-------------------------------------------------列拼接(连接查询)-----------------------------------------
—只显示匹配的
select * from 表名 inner join 表名 on 条件
—显示左边表全部,右边表的匹配
select * from 表名 left join 表名 on 条件
—显示右边表全部,左边表的匹配
select * from 表名 right join 表名 on 条件
--------------------------------------------------case查询--------------------------------------------------
select
*,列名=case
when 条件 then 列值
when 条件 then 列值
else 列值
end
from 表名
---------------------------------------------------子查询----------------------------------------------------
select * from 表名 where 列名=(select 列名 from 表名 条件)