数据库常用的基本操作
生活随笔
收集整理的這篇文章主要介紹了
数据库常用的基本操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??1?--------------------------------對數據庫操作-----------------------------------
??2?--建立數據庫
??3?create?database?Test
??4?
??5?--使用數據庫
??6?use?Test
??7?
??8?--刪除數據庫
??9?DROP?DATABASE?Test
?10?
?11?--------------------------------對表操作----------------------------------------
?12?--建立表
?13?create?table?Person
?14?(
?15?????ID?int?primary?key?not?null,--編號
?16?????Name?varchar(10)?not?null,--姓名
?17?????Birth?datetime?not?null,--生日
?18?????Address?varchar(50),--地址
?19?????Phone?varchar(50),--電話
?20?)
?21?create?table?person1
?22?(
?23?????pID?int?primary?key?not?null,--編號
?24?????Sex?varchar(10)?not?null,--性別
?25?)
?26?
?27?--創建新表,并從原有表中復制指定內容
?28?create?table?person2
?29?(
?30?????pID?varchar(2),
?31?????pName?varchar(30),
?32?????pSex?varchar(2),
?33?)
?34?insert?into?person2?select?*?from?person?where?personSex='男'
?35?
?36?--刪除表
?37?drop?table?person
?38?
?39?--增加列
?40?alter?table?person?add?State?varchar(10)
?41?
?42?--刪除列
?43?alter?table?person?drop?column?State
?44?
?45?--修改列的數據類型
?46?alter?table?person?alter?column?State?varchar(20)
?47?
?48?--增加行
?49?insert?into?Person?values(1,'張超',1983-6-28,'北京','010-88256483')
?50?insert?into?Person?values(2,'李華',1983-5-20,'北京','010-88462582')
?51?insert?into?Person?values(3,'王梅',1983-2-23,'梅州','0753-6570211')
?52?insert?into?person1?values(1,'男')
?53?insert?into?person1?values(2,'男')
?54?insert?into?person1?values(3,'女')
?55?
?56?--修改行
?57?update?person?set?Name='張英'?where?ID=1
?58?
?59?--刪除行
?60?delete?person?where?ID=2
?61?
?62?--刪除表中所有數據
?63?truncate?table?person
?64?
?65?--簡單查詢
?66?select?*?from?person
?67?select?*?from?person1
?68?
?69?--條件查詢
?70?select?*?from?person?where?ID=1
?71?select?*?from?person?where?ID=1?and?ID?is?not?null
?72?select?*?from?person?where?ID=1?or?ID?is?null
?73?select?*?from?person?where?ID?is?not?null
?74?select?*?from?person?where?ID?between?0?and?3?and?contains(Address,'北京')?--從屬運算
?75?select?*?from?person?where?ID>0?and?ID<=3
?76?select?*?from?person?where?Address?like?'北_'??--通配一個字符
?77?select?*?from?person?where?ID?in(1,2,3)???--從屬運算
?78?select?*?from?person?where?Address?like?'北%'??--通配多個字符
?79?select?*?from?person?where?ID?like?'%'
?80?select?count(*)?from?person?where?ID=3??--返回滿足條件的行的個數
?81?
?82?--聯合查詢
?83?select?*?from?person?where?ID>0?union?select?*?from?person?where?ID<=3
?84?select?(p.Name,p1.Sex)?from?(person?p,person?p1)--給不同的表取別名并聯合查詢指定列
?85?select?*?from?operator?intersect?select?*?from?operator2?--intersect相交,返回兩個表中共有的行
?86?
?87?--表關聯查詢
?88?select?distinct?*?from?person?as?p????--distinct防止重復
?89?join?person1?as?p1
?90?on?ID=pID
?91?where?ID=1
?92?
?93?--限制查詢
?94?select?top?10?percent?with?ties?*?from?person?order?by?ID?asc--升序
?95?select?top?10?percent?with?ties?*?from?person?order?by?ID?desc--降序
?96?select?top?10?*?from?person?order?by?ID?asc
?97?select?*?from?person?group?by?ID?having?ID<3??--進行分組查詢
?98?
?99?--對查詢進行輸出控制
100?select?ID?as?編號,Name?as?姓名,Birth?as?生日,Address?as?地址,Phone?as?電話
101?from?person?order?by?ID?desc
102?
103?--------------------------------對視圖操作----------------------------------------
104?--可以象操作表一樣操作視圖,但應少用insert?update?delete等語句
105?
106?--創建視圖
107?create?view?myView
108?as
109?select?*?from?Orders
110?
111?--修改視圖
112?alter?view?myView
113?as
114?select?*?from?Orders
115?
116?--刪除視圖
117?drop?view?myView
118?
119?--查看視圖
120?select?top?10?*?from?myView?where?OrderID>=10250--顯示頭10條記錄
121?
122?-----------------------------對索引的操作--------------------------------------
123?use?test
124?select?*?from?person
125?
126?--創建索引
127?create?index?index_person?on?person(ID)
128?
129?--創建unique類型的索引
130?create?unique?index?index_person?on?person(ID)
131?
132?--刪除索引
133?drop?index?person.index_person?
134?
135?-----------------------------對存儲過程的操作----------------------------------
136?--建立存儲過程
137?create?procedure?dbo.GetDataById
138?(
139?????@id?int
140?)
141?as
142?select?*?from?T_Person?where?id=@id????
143?return
144?
145?--修改存儲過程
146?alter?procedure?dbo.GetDataById
147?(
148?????@id?int
149?)
150?as
151?select?*?from?T_Person?where?id=@id????
152?return
153?
154?--執行存儲過程
155?exec?GetDataById?2
156?
157?--刪除存儲過程
158?drop?procedure?dbo.GetDataById
??2?--建立數據庫
??3?create?database?Test
??4?
??5?--使用數據庫
??6?use?Test
??7?
??8?--刪除數據庫
??9?DROP?DATABASE?Test
?10?
?11?--------------------------------對表操作----------------------------------------
?12?--建立表
?13?create?table?Person
?14?(
?15?????ID?int?primary?key?not?null,--編號
?16?????Name?varchar(10)?not?null,--姓名
?17?????Birth?datetime?not?null,--生日
?18?????Address?varchar(50),--地址
?19?????Phone?varchar(50),--電話
?20?)
?21?create?table?person1
?22?(
?23?????pID?int?primary?key?not?null,--編號
?24?????Sex?varchar(10)?not?null,--性別
?25?)
?26?
?27?--創建新表,并從原有表中復制指定內容
?28?create?table?person2
?29?(
?30?????pID?varchar(2),
?31?????pName?varchar(30),
?32?????pSex?varchar(2),
?33?)
?34?insert?into?person2?select?*?from?person?where?personSex='男'
?35?
?36?--刪除表
?37?drop?table?person
?38?
?39?--增加列
?40?alter?table?person?add?State?varchar(10)
?41?
?42?--刪除列
?43?alter?table?person?drop?column?State
?44?
?45?--修改列的數據類型
?46?alter?table?person?alter?column?State?varchar(20)
?47?
?48?--增加行
?49?insert?into?Person?values(1,'張超',1983-6-28,'北京','010-88256483')
?50?insert?into?Person?values(2,'李華',1983-5-20,'北京','010-88462582')
?51?insert?into?Person?values(3,'王梅',1983-2-23,'梅州','0753-6570211')
?52?insert?into?person1?values(1,'男')
?53?insert?into?person1?values(2,'男')
?54?insert?into?person1?values(3,'女')
?55?
?56?--修改行
?57?update?person?set?Name='張英'?where?ID=1
?58?
?59?--刪除行
?60?delete?person?where?ID=2
?61?
?62?--刪除表中所有數據
?63?truncate?table?person
?64?
?65?--簡單查詢
?66?select?*?from?person
?67?select?*?from?person1
?68?
?69?--條件查詢
?70?select?*?from?person?where?ID=1
?71?select?*?from?person?where?ID=1?and?ID?is?not?null
?72?select?*?from?person?where?ID=1?or?ID?is?null
?73?select?*?from?person?where?ID?is?not?null
?74?select?*?from?person?where?ID?between?0?and?3?and?contains(Address,'北京')?--從屬運算
?75?select?*?from?person?where?ID>0?and?ID<=3
?76?select?*?from?person?where?Address?like?'北_'??--通配一個字符
?77?select?*?from?person?where?ID?in(1,2,3)???--從屬運算
?78?select?*?from?person?where?Address?like?'北%'??--通配多個字符
?79?select?*?from?person?where?ID?like?'%'
?80?select?count(*)?from?person?where?ID=3??--返回滿足條件的行的個數
?81?
?82?--聯合查詢
?83?select?*?from?person?where?ID>0?union?select?*?from?person?where?ID<=3
?84?select?(p.Name,p1.Sex)?from?(person?p,person?p1)--給不同的表取別名并聯合查詢指定列
?85?select?*?from?operator?intersect?select?*?from?operator2?--intersect相交,返回兩個表中共有的行
?86?
?87?--表關聯查詢
?88?select?distinct?*?from?person?as?p????--distinct防止重復
?89?join?person1?as?p1
?90?on?ID=pID
?91?where?ID=1
?92?
?93?--限制查詢
?94?select?top?10?percent?with?ties?*?from?person?order?by?ID?asc--升序
?95?select?top?10?percent?with?ties?*?from?person?order?by?ID?desc--降序
?96?select?top?10?*?from?person?order?by?ID?asc
?97?select?*?from?person?group?by?ID?having?ID<3??--進行分組查詢
?98?
?99?--對查詢進行輸出控制
100?select?ID?as?編號,Name?as?姓名,Birth?as?生日,Address?as?地址,Phone?as?電話
101?from?person?order?by?ID?desc
102?
103?--------------------------------對視圖操作----------------------------------------
104?--可以象操作表一樣操作視圖,但應少用insert?update?delete等語句
105?
106?--創建視圖
107?create?view?myView
108?as
109?select?*?from?Orders
110?
111?--修改視圖
112?alter?view?myView
113?as
114?select?*?from?Orders
115?
116?--刪除視圖
117?drop?view?myView
118?
119?--查看視圖
120?select?top?10?*?from?myView?where?OrderID>=10250--顯示頭10條記錄
121?
122?-----------------------------對索引的操作--------------------------------------
123?use?test
124?select?*?from?person
125?
126?--創建索引
127?create?index?index_person?on?person(ID)
128?
129?--創建unique類型的索引
130?create?unique?index?index_person?on?person(ID)
131?
132?--刪除索引
133?drop?index?person.index_person?
134?
135?-----------------------------對存儲過程的操作----------------------------------
136?--建立存儲過程
137?create?procedure?dbo.GetDataById
138?(
139?????@id?int
140?)
141?as
142?select?*?from?T_Person?where?id=@id????
143?return
144?
145?--修改存儲過程
146?alter?procedure?dbo.GetDataById
147?(
148?????@id?int
149?)
150?as
151?select?*?from?T_Person?where?id=@id????
152?return
153?
154?--執行存儲過程
155?exec?GetDataById?2
156?
157?--刪除存儲過程
158?drop?procedure?dbo.GetDataById
轉載于:https://www.cnblogs.com/anllin/articles/2007115.html
總結
以上是生活随笔為你收集整理的数据库常用的基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OnlineJudge在线判题系统
- 下一篇: qt 连接mysql