SQL常用函式

2021-09-05 12:54:58 字數 2122 閱讀 3838

新建表:

create table [表名] (

[自動編號字段] int identity (1,1) primary key ,

[欄位1] nvarchar(50) default \'預設值\' null ,

[欄位2] ntext null ,

[欄位3] datetime,

[欄位4] money null ,

[欄位5] int default 0,

[欄位6] decimal (12,4) default 0,

[欄位7] image null , )

刪除表:

drop table [表名]

插入資料:

insert into [表名] (欄位1,欄位2) values (100,\'51windows.net\')

刪除資料:

delete from [表名] where [欄位名]>100

更新資料:

update [表名] set [欄位1] = 200,[欄位2] = \'51windows.net\' where [欄位三] = \'haiwa\'

新增字段:

alter table [表名] add [欄位名] nvarchar (50) null

刪除字段:

alter table [表名] drop column [欄位名]

修改字段:

alter table [表名] alter column [欄位名] nvarchar (50) null

重新命名表:

sp_rename \'表名\', \'新錶名\', \'object\'

新建約束:

alter table [表名] add constraint 約束名 check ([約束字段] <= \'2000-1-1\')

刪除約束:

alter table [表名] drop constraint 約束名

新建預設值

alter table [表名] add constraint 預設值名 default \'51windows.net\' for [欄位名]

刪除預設值

alter table [表名] drop constraint 預設值名

刪除sql server 中的日誌,減小資料庫檔案大小

dump transaction 資料庫名 with no_log

backup log 資料庫名 with no_log

dbcc shrinkdatabase(資料庫名)

exec sp_dboption \'資料庫名\', \'autoshrink\', \'true\'

extract()日期函式:

注: 19931209 距 19960823 2年8個月14天

select extract(year from age('19931209'::date,'19960823'::date)) --返回指定日期到當期日期的年數

-2select extract(month from age('19931209'::date,'19960823'::date)) --返回指定日期到當期日期的月數

-8select extract(day from age('19931209'::date,'19960823'::date)) --返回指定日期到當期日期的天數

-14select extract('19960831'::date) --返回指定日期的年份

1996

select extract('19960831'::date) --返回指定日期的月份

08select extract('19960831'::date) --返回指定日期的日期

31

sql常用函式

格式 cast expression as data type 該函式主要用於字段型別轉換 select cast id as int from table 格式 mod nexp1,nexp2 如查詢欄位id對5取余為1的資料 select from table where mod id,5 1 ...

SQL常用函式

2020雲棲大會 阿里雲海量offer來啦!投簡歷 贏阿里雲限量禮品及阿里雲aca認證免費考試資格!sql常用函式 函式介紹 函式分類 字串函式 數值函式 日期和時間函式 流程函式 其他常用函式 函式介紹 事先提供好的一些功能可以直接使用 函式可以用在select語句及其子句 也可以用在update...

SQL常用函式

substr string,start,length 引數 必須 引數說明 string 必須 指定的要擷取的字串 start 必須 規定在字串的何處開始 正數 在字串的指定位置開始 負數 在從字串結尾的指定位置開始 0 在字串中的第乙個字元處開始 length 可選 指定要擷取的字串長度,預設時返...