--创建数据库
create database 数据库名;
--删除数据库
--drop database 数据库名;
-- 使用数据库 info
use 数据库名;
--创建表
--create table 表名(id int primary key auto_increment, name varchar(255), age int, score int);
create table 表名(id int primary key, name varchar(255), age int, score int);
--删除表
--drop table 表名;
--查询表结构
exec sp_help 表名;
--插入数据
insert into 表名(id, name, age) values(1, 'mike', 18);
insert into 表名(id, name, age, score) values(2, 'lucy', 22, 90);
insert into 表名(id, name, age, score) values(3, 'Tom', 20, 78);
--更新数据
update student set 列名 = 90 where id = 3;
--某一列复制到另一列
update 表 set 列1 = 列2;
--设置某一列统一值
update 表 set 列名 = 值;
--查找数据
select * from 表名;
select 列名 from 表名 where 列名 = 'mike';
--删除数据
delete from 表名 where name = 'mike';
--修改字段类型
alter table 表名 alter column 字段名 type not null;
--添加字段并设置默认值(注意添加了默认约束)
alter table test drop constraint DF_TEST_Approve1_1BFD2C07;
alter table test drop column Approve1;
--修改字段类型以及表的约束
alter table 表名 alter column 列名 nvarchar(50) null;
--主键自增情况下执行插入语句
set IDENTITY_INSERT ack_data on;
insert into ack_data(id,variable) values (100,str);
set IDENTITY_INSERT ack_data off;
select * from ack_data;
--批量更新字段的值
update 表名 set 列名 = '11',列名 = '22';
--获取不重复的某一列信息
select distinct 字段名 from test;
select distinct 字段名 from 表名 WHERE Dept = '车载电源'
select distinct 字段1,字段2 from 表名;//根据两个字段去重,distinct必须放在开头
--设置主键约束
alter table 表名 add primary key(id);//不带名字约束的联合主键
alter table 表名 add constraint PK_Union _id primary key(id,日期);//带名字约束的联合主键
--设置字段唯一约束
alter table 表名 add constraint 约束名 unqiue(字段名);
--删除约束
alter table 表名 drop primary key;
alter table 表名 drop constraint 约束名;
--添加约束
alter table 表名 add constraint 约束名 约束类型(列名,列名);
--查看字段约束
select * from information_schema.constraint_column_usage where TABLE_NAME = '表名';
--设置某一列不为空
alter table 表名 alter column 列名类型 not null;
--查找某一列重复的记录
select * from 表名 where 列名 in (select 列名 from group by 列名 having count(列名)>1);
select 列名 from 表名 group by 列名 having (count(*))>1;
select 列名 from 表名 group by 列名 having (count(user_name))>1;
--查找某几列重复的记录
select 列名1,列名2 from 表名 group by 列名1,列名2 having count(*)>1;
--增加列
alter table 表名 add 列名 类型;
--获取数据库的所有表名
use 数据库名
go
select name from sysobjects where xtype = 'u';
--查询该数据库有几张表
select count(*) from sysobjects where xtype='u';
--获取所有表的所有列名
select name from syscolumns where id=ovject_id('UserInfo');//列的顺序是按首字母排序
select COLUMN_NAME from INFROMATION_SCHEMA.columns where TABLE_NAME ='表名' and table_schema = '数据库名';//列的顺序是表中的顺序
select * from INFROMATION_SCHEMA.columns where TABLE_NAME = '表名';
--根据表名获取列数
select count(name) from syscolumns where id = object_id('UserInfo');
--获取表的记录数
select count(*) from 表名;//数据量大的时候慢
EXEC sp_spaceused 'sh_help';//存储过程语句在代码中不返回数据
select rows from sysindexes where id = object_id('表名') and indid in (0,1);//有可能不精确
--快速统计每个表的记录数
select schema_name(t.schema_id) as [Schema],t.name as TABLE_NAME,i.rows as [RowCount] from sys.tables as t,sysindexes as i where t.object_id = i.id and i.indid <= 1;
--查看数据库中的所有触发器
select * from sysobjects where xtype= 'TR';
--查看触发器的内容
exec sp_helptext '触发器名称';
--查看触发器的属性
exec sp_helptrigger 表名;
--禁用触发器
alter table 表名 disable trigger 触发器名称;
--启用触发器
alter table 表名 enable trigger 触发器名称;
如果有多个触发器,则各个触发器名称之间用英文逗号隔开
如果把"触发器名称"换成"ALL"则表示禁用或启用该表得全部触发器
--删除触发器
drop trigger 触发器名称;
--转换日期格式
update 表名 set 次数 = 3 where ID = 'wangzitong' and 日期 = CONVERT(date,GETDATE(),23);
-- 对多个字段进行模糊查询
select * from 表名 where concat(字段1,字段2) like '%关键字%';