SQL Server中常用SQL語句


--創建數據庫
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 '%關鍵字%';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM