當前位置:編程學習大全網 - 腳本源碼 - sql常用語句寫法

sql常用語句寫法

SQL 基本操作命令 創建數據庫create database 數據庫名切換數據庫use database 數據庫名刪除數據庫drop database 數據庫名 將數據庫設為只讀execute sp_dboption '數據庫名','rend only','true' 將數據庫設為自動收縮execute sp_dboption '數據庫名','autoshrink','true'將數據庫設為單獨訪問execute sp_dboption '數據庫名','single user' 收縮數據庫:dbcc shrinkdatabase(數據庫名,未用空間百分比) 創建表create table 表名(列名 數據類型,列名 數據類型) 建表時創建主鍵create table 表名(列名 數據類型 primary key,列名 數據類型)建表後創建主鍵alter table 表名 add constraint pk_表名 primary key(列名) 建表後刪除主鍵alter table 表名 drop constraint pk_表名 建表時創建唯壹約束create table 表名(列名 數據類型 unique,列名 數據類型)建表後創建唯壹約束alter table 表名 add constraint u_表名 unique(列名) 建表後刪除唯壹約束alter table 表名 drop constraint u_表名 建表時創建檢查約束create table 表名(列名 數據類型 check(條件),列名 數據類型)建表後創建檢查約束alter table 表名 add constraint ck_表名 check(條件) 建表後刪除檢查約束alter table 表名 drop constraint ck_表名 建表時創建默認約束create table 表名(列名 數據類型 default(默認值),列名 數據類型)建表後創建默認約束alter table 表名 add constraint df_表名 default(默認值) for 列名 建表後刪除默認約束alter table 表名 drop constraint df_表名 建表時創建外鍵約束create table 表名(列名 數據類型 foreign key references 外表名(主鍵),列名 數據類型)建表後創建外鍵約束alter table 表名 add constraint fk_表名 foreign key(列名) references 外表名(主鍵) 建表後刪除外鍵約束alter table 表名 drop constraint fk_表名 刪除表drop table 表名設置列值自動編號create table 表名(列名 數據類型 int identity(起始值,步長),列名 數據類型) 修改表中列的數據類型alter table 表名[alter column 列名 數據類型]在表中添加壹個新列alter table 表名[add 列名 數據類型]刪除表中的某壹列alter table 表名[drop column 列名] 輸入數據insert into 表名 values(對應列的值) 更新數據update 表名 set 新值 where 條件刪除數據delete from 表名 where 條件刪除表中所有數據truncate table 表名 將現有表中的數據添加到另壹個表insert 目標表名 select 源表列名 from 源查詢所有數據select * from 表名按條件查詢數據select * from 表名 where 條件 按條件查詢某列不重復數據select distinct 列名 from 表名 where 條件按升序排列查詢結果select * from 表名 order by 列名按降序排列查詢結果select * from 表名 order by 列名 desc 按條件查詢數據並排序select * from 表名 where 條件 order by 列名 在查詢結果中自定義列名select 新列名=原列名 from 表名 where 條件在查詢結果中返回最前面的行select top 行數 * from 表名在查詢結果中返回最前面的行數的百分比select top 百分比 percent * from 表名查詢列中所有數值的和select 新列名=sum(列名) from 表名 where 條件查詢列中所有數值的平均值select 新列名=avg(列名) from 表名 where 條件查詢列中非空值的數目select 新列名=count(列名) from 表名查詢表中非空值的數目select 新列名=count(*) from 表名查詢列中的最大值select 新列名=max(列名) from 表名查詢列中的最小值select 新列名=min(列名) from 表名對查詢結果按條件進行分組select 聚合函數(列名) from 表名 group by 列名 having 條件模糊查詢select * from 表名 where 列名 like ‘字符通配符’查詢表中包含指定值的所有行select * from 表名 where 列名 in ('值')查詢表中不包含指定值的所有行select * from 表名 where 列名 not in ('值')查詢表中列的數值在數值1到數值2之間的所有行select * from 表名 where 列名 between 數值1 and 數值2查詢表1和表2中包含相同列的所有行select * from 表1 inner join 表2 on 表1.列=表2.列 where 條件 我空間裏有,備忘用的

  • 上一篇:魔獸爭霸 小T , 資料
  • 下一篇:摩羯座男生真正的性格
  • copyright 2024編程學習大全網