SQL server 基本語句


--查詢數據庫是否存在

if exists ( select * from sysdatabases where [name]='TestDB')

print 'Yes, the DB exists'

else

print 'No, need a new one?'

 

--新建一個數據庫

create database TestDB on

(

    name = 'TestData',

    filename = 'G:\DBS\KeyTest.mdf',

    size = 3,

    filegrowth = 2

)

log on

(

    name = 'TestLog',

    filename = 'G:\DBS\KeyTest.ldf',

    size = 3,

    filegrowth = 10

)

 

--drop database TestDB

 

use TestDB

go

 

--新建一個表

create table [Scores]

(

    [ID] int identity(1,1) primary key,

    [Student] varchar(20) ,

    [Subject] varchar(30),

    [Score] float

)

 

--drop table [Scores]

 

--修改表中的一列

alter table Scores alter column [Student] varchar(20) not null

 

--新增一列

alter table Scores add Birthday datetime 

 

--刪除一列

alter table Scores drop column Birthday

 

--往表中插入單條數據,方法:帶列名

insert into Scores(Student,Subject,Score)

values('張三','語文','90')

 

--往表中插入單條數據,方法:不帶列名,但要求值的類型要和列字段類型對應

insert into Scores

values('張三','英語','95')

 

--插入多條數據:union或者union all

insert into Scores(Student,Subject,Score)

select '李四','語文','89'

union all

select '李四','英語','78'

 

--刪除表中數據,沒有條件時,刪除所有

delete from Scores where ID in(7,8)

 

--修改表中數據

update Scores 

set Student='王五',Score='94'

where ID=10

 

--查看數據

select * from Scores

 

--查看表中最大的identity

select @@identity

 

--查找兩個表中列的值是否相同,相同則不顯示,不相同的則查出結果

select * from B_PowerStation a where not exists(select * from B_PowerPile b where a.ID=b.ID)

 

--或者利用dbcc命令查看表中最大的identity

dbcc checkident('Scores',noreseed)

 

--創建視圖,全部省略視圖的屬性列名,由子查詢目標列的字段組成

create view StudentView

as

select Student,Subject,Score

from Scores

 

--加上with check option,以后對視圖的操作(增,改,刪,查)都會自動加上where ID>3

/*

create view StudentView

as

select Student,Subject,Score

from Scores

where ID>3

with check option

*/

 

--創建視圖,全部定義屬性列名,需要定義列名的情況:

----某個目標列(子查詢)不是單純的屬性列,而是聚集函數或列表達式

----多表連接時選出了幾個同名列

----需要在視圖中為某個列啟用新的更合適的名字

create view IS_Student(Student,Subject,MaxScore)

as

select Student,Subject,Score

from Scores

where Score=(select max(Score) from Scores)

 

 

--查詢視圖,和基本表完全樣,只不過如果視圖中有with check option,會自動加上那個條件

select * 

from StudentView

 

--查詢自定義列名的視圖

select * 

from IS_Student

 

--對視圖的insert/delete/update,和對基本表的操作一樣,並且最終都是用RDBMS自動轉換為對基本表的更新

--並不是所有的視圖都是可更新的,因為有些視圖的更新不能有意義的轉換成對相應基本表的更新

 

--刪除視圖

drop view StudentView

 

1.  SQL常用命令使用方法

(1)數據記錄篩選

select*from Products查詢出Products表里面的所有信息

select ProductID,ProductName from Products查詢出Products表里面所有ProductID,ProductName

select ProductID,ProductName from Products where ProductID=1查詢出Products表里ProductID=1的所有ProductIDProductName

select* from employee where fname='Paul' and job_id=5 查詢出employee表中fname=Paul並且job_id=5的所有記錄

select*from Products where ProductID in(4,5,6)查詢出Products表中ProductID,5,6的所有信息

select*from Products where UnitPrice>10 and UnitPrice<30 order by UnitPrice查詢出Products表中<UnitPrice<30的所有信息並按照UnitPrice的大小由小到大排序

select*from Products where UnitPrice between 10 and 30 order by UnitPrice上面的另外一種寫法

select * from Employees where FirstName like 'A%'查詢出EmployeesFirstName里面第一個字母是A的所有人信息

select*from Employees where FirstName like '%A%'查詢出EmployeesFirstName里面中間有A的所有人信息

select*from Employees where FirstName like '%A'查詢出EmployeesFirstName里面最后一個字母是A的所有人信息

select count(*) from Employees查詢出Employees表中的所有記錄數

select min(Unitprice)from Products查詢出Products表中Unitprice的最小值

select max(Unitprice)from Products查詢出Products表中Unitprice的最大值

select avg(Unitprice)from Products查詢出Products表中Unitprice的平均值

select sum(Unitprice)from Products查詢出Products表中Unitprice的總和

select * from Products where Unitprice> (select avg(Unitprice) from Products)有子查詢查找出比平均值高的商品信息

select top 5* from Products查詢出前五條的記錄信息

select distinct [name] from Category 查出Category 中不重復的name

select count(distinct name) from Category 查出Category 中不重復的name的數量

 

(2) 更新數據記錄

sql="update 數據表set 字段名=字段值where 條件表達式"

sql="update 數據表set 字段=,字段=……字段n=n where 條件表達式"

 

(3) 刪除數據記錄

sql="delete from 數據表where 條件表達式"

sql="delete from 數據表" (將數據表所有記錄刪除)

 

(4) 添加數據記錄

sql="insert into 數據表(字段,字段,字段…) values (,,…)"

sql="insert into 目標數據表select 字段名from 源數據表" (把源數據表的記錄添加到目標數據表)

 

(5) 數據記錄統計函數

AVG(字段名得出一個表格欄平均值

COUNT(*&brvbar;字段名對數據行數的統計或對某一欄有值的數據行數統計

MAX(字段名取得一個表格欄最大的值

MIN(字段名取得一個表格欄最小的值

SUM(字段名把數據欄的值相加

引用以上函數的方法

sql="select sum(字段名) as 別名from 數據表where 條件表達式"

set rs=conn.excute(sql)

 rs("別名"獲取統的計值其它函數運用同上

 

(6) 數據表的建立和刪除

CREATE TABLE 數據表名稱(字段 類型(長度),字段 類型(長度) …… )

CREATE TABLE tab01(name varchar(50),datetime default now())

DROP TABLE 數據表名稱 (永久性刪除一個數據表)

 

2. 記錄集對象的方法

rs.movenext 將記錄指針從當前的位置向下移一行

rs.moveprevious 將記錄指針從當前的位置向上移一行

rs.movefirst 將記錄指針移到數據表第一行

rs.movelast 將記錄指針移到數據表最后一行

rs.absoluteposition=N 將記錄指針移到數據表第N

rs.absolutepage=N 將記錄指針移到第N頁的第一行

rs.pagesize=N 設置每頁為N條記錄

rs.pagecount 根據 pagesize 的設置返回總頁數

rs.recordcount 返回記錄總數

rs.bof 返回記錄指針是否超出數據表首端true表示是false為否

rs.eof 返回記錄指針是否超出數據表末端true表示是false為否

rs.delete 刪除當前記錄但記錄指針不會向下移動

rs.addnew 添加記錄到數據表末端

rs.update 更新數據表記錄

 

3.  排序

desc就是用於查詢出結果時候對結果進行排序是降序排序asc就是升序。。要用與order by一起用 

例如select * from student order by id desc; 就是把選出的結果經過id從大到小排序把資源返回

還可以select * from student order  by age desc,id desc;“,”號隔開多個排序條件這樣先按age 再按 id就是說先按age從大到小排序如果有相同年齡的那么相同年齡的學生再按他們的id從大到小排序

DESC指排序時按降序排序ASC是升序

 

約束篇:

1.主鍵約束

要對一個列加主鍵約束的話這列就必須要滿足的條件就是分空

因為主鍵約束就是對一個列進行了約束約束為非空不重復

以下是代碼   要對一個列加主鍵列名為id,表名為emp

格式為

alter table 表格名稱 add constraint 約束名稱 增加的約束類型 列名

例子

alter table emp add constraint ppp primary key (id)

2.check約束

就是給一列的數據進行了限制

比方說年齡列的數據都要大於的

表名emp 列名age

格式

alter table 表名稱 add constraint 約束名稱 增加的約束類型 列名

例子

alter table emp add constraint xxx check(age>20)

3.unique約束

這樣的約束就是給列的數據追加的不重復的約束類型

格式

alter table 表名 add constraint 約束名稱 約束類型列名

比方說可以給ename列加個uniqueename列的數據不重復

例子

alter table emp add constraint qwe unique(ename)

4.默認約束

意思很簡單就是讓此列的數據默認為一定的數據

格式

alter table 表名稱 add constraint 約束名稱 約束類型 默認值 for 列名

比方說emp表中的gongzi列默認為

alter table emp add constraint jfsd default 10000 for gongzi

5.外鍵約束

這個有點難理解了外鍵其實就是引用

因為主鍵實現了實體的完整性

外鍵實現了引用的完整性

應用完整性規定所引用的數據必須存在

其實就是個引用

比方說一個表名稱叫dept 里面有列數據 一列是ID一列是ENAME

id表示產品的編號

ename表示產品的名稱

另外一個表格名稱是emp 里面有列數據一列是ID 一列是DID

id:表示用戶號

did表示購買的產品號

要讓emp表中的did列去引用dept表中的id

可以用下面的方法

格式

alter table 表名 add constraint 約束名稱 約束類型 (列名) references 被引用的表名稱列名

例子

alter table emp add constraint jfkdsj foreign key (did) references dept (id

 

修改約束

 

 


免責聲明!

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



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