SQL數據庫的語句整理

歡迎來到unity學習unity培訓unity企業培訓教育專區,這裏有很多U3D資源U3D培訓視頻U3D教程我們致力於打造業內unity3d培訓學習第一品牌。

 

      1. 建立數據庫:create database <數據庫名>

      2. 利用數據庫:  use <數據庫名>

      3. 建立數據表:


      create table 表名(
      id int identity(101,1) primary key,
      name varchar(20) not null,
      password varchar(10)
       ) 

 

      4.刪除數據庫:drop database third

 

      5.刪除表:drop table student

 

      6. 查詢所有信息:select * from <表名>

 

      7.插入一行數據insert  into <表名>[(列名)] values(值列表)

 

      8.插入多行數據:insert into<表名>(列名)

           insert into <表名>(列名)

           select <列值>union

           select <列值>union

 

      9.更新數據:

            update <表名>set<列名=更新值> [where<更新條件>]

 

      10.刪除數據: delete from<表名>[where<刪除條件>]

 

      11.添加一列:alter table 表名 add 列名 類型(長度) null

 

      12:更改一個列的類型:alter table 表名 alter column 列名 數據類型(長度)

 

      13.刪除一列:alter table 表名 drop column 列名

 

      14.主鍵約束:alter table 表名 add constraint 主鍵別名 primary key (主鍵列

 

      15.唯一鍵約束:alter table 表名 add constraint 唯一鍵別名 unique (唯一鍵列)

 

      16.默認鍵約束: alter table 表名  add constraint 默認鍵別名 default (‘默認值’) for 默認鍵

 

      17.檢查鍵約束:alter table 表名 add constraint 檢查鍵別名check(stuAge>=15 and stuAge<=40)

 

      18.添加檢查鍵:teacherusers表添加主外關聯

          alter table score add constraint t_fk foreign key(uid) references users(id)

 

      19.刪除約束:alter table 表名  drop constraint 約束別名

 

      20.查詢部分數據:select id,name from users where name='張三' --查詢表中name爲‘張三’的數據

 

      21.去掉重複字段查詢記錄:select distinct name from student

 

      22.合併查詢(合併兩表中相同的字段):select * from student union select * from score

 

      23.用AS來命名列:select id as 編號,name as 姓名 from users

 

      24.用 = 來命名列:select 編號 =id ,姓名=name  from users

 

      25.查詢空行:select id, name from users where password is null

 

      26.查詢非空行select name from users where name is not null

 

      27.使用常量列(默認值):select name as  姓名 ,'密碼' as password from users 

 

      28.限制固定行數:select top 3 * from users

 

      29.返回百分之多少行:select top 50 percent * from users

 

      30.升序:select * from users order by idselect * from users order by id asc

 

      31.降序: select * from users order by id desc

 

      32.按多列排序(當排序的值相同時,按第二個字段排序):select * from users order by name,id

 

      33.模糊查詢Like:SELECT * FROM 數據表 WHERE 編號 LIKE 數值

 

      34.模糊查詢is null:SELECT * FROM 數據表 WHERE 編號 IS NULL 數值

 

      35.模糊查詢Between:SELECT StudentID, Score FROM SCore WHERE Score BETWEEN 60 AND 80

 

      36.模糊查詢in:SELECT SName AS 學員姓名,SAddress As 地址 FROM Students WHERE SAddress IN ('北京','廣州','上海')

 

      37.sum總和:select sum(age) as '年齡總和' from student --計算student中所有學生年齡的總和

 

      38.avg平均:select avg(id) as 平均 from users  --計算user中所有id的平均數

 

      39.Mix,Min最大最小值:select max(id) as 最大,min(id) as 最小 from users --計算出userid的最大值和最小值

 

      40.country:SELECT COUNT(*)  AS 及格人數 From Score WHERE Score>=60

 

      41.分組查詢Group by: select id,avg(id) as 平均 from users group by id

 

      42.內連接查詢(跟表的位置無關):

           SELECT Students.SName, Score.CourseID, Score.Score

           FROM  Students,Score

           WHERE  Students.SCode = Score.StudentID

43.左外連接查詢(跟表的位置有關):

           SELECT  S.SName,C.CourseID,C.Score 

           From  Students AS S

           LEFT JOIN  Score AS C

           ON  C.StudentID = S.SCode

 

44.右外連接查詢(跟表的位置有關):

           SELECT Titles.Title_id, Titles.Title, Publishers.Pub_name

           FROM titles 

           RIGHT OUTER JOIN Publishers 

           ON Titles.Pub_id = Publishers.Pub_id

 

 

 

 

 

 

 

 更多精彩請看http://www.gopedu.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章