標籤:中間 abs 返回 dep 代碼 alter als bof 資料庫知識
1.主鍵約束:
要對一個列加主鍵約束的話,這列就必須要滿足的條件就是非空
因為主鍵約束:就是對一個列進行了約束,約束為(非空、不重複)
以下是代碼 要對一個列加主鍵,列名為id,表名為emp
格式為:
alter table 表格名稱 add constraint 約束名稱 增加的約束類型 (列名)
例子:
alter table emp add constraint ppp primary key (id)
2.check約束:
就是給一列的資料進行了限制
比方說,年齡列的資料都要大於20的
表名(emp) 列名(age)
格式:
alter table 表名稱 add constraint 約束名稱 增加的約束類型 (列名)
例子:
alter table emp add constraint xxx check(age>20)
3.unique約束:
這樣的約束就是給列的資料追加的不重複的約束類型
格式:
alter table 表名 add constraint 約束名稱 約束類型(列名)
比方說可以給ename列加個unique,讓ename列的資料不重複
例子:
alter table emp add constraint qwe unique(ename)
4.預設約束:
意思很簡單就是讓此列的資料預設為一定的資料
格式:
alter table 表名稱 add constraint 約束名稱 約束類型 預設值) for 列名
比方說:emp表中的gongzi列預設為10000
alter table emp add constraint jfsd default 10000 forgongzi
5.外鍵約束:
這個有點難理解了,外鍵其實就是引用
因為主鍵實現了實體的完整性,
外鍵實現了引用的完整性,
應用完整性規定,所引用的資料必須存在!
其實就是個引用,
比方說一個表名稱叫dept 裡面有2列資料 一列是ID一列是ENAME
id:表示產品的編號
ename:表示產品的名稱
另外一個表格名稱是emp 裡面有2列資料,一列是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)
SQL常用命令使用方法:
資料記錄篩選:
select*from Products:查詢出Products表裡面的所有資訊
select ProductID,ProductName from Products:查詢出Products表裡面所有的ProductID,ProductName
select ProductID,ProductName from Productswhere ProductID=1:查詢出Products表裡面ProductID=1的所有ProductID和ProductName
select* from employee where fname=‘Paul‘ andjob_id=5 :查詢出employee表中fname=Paul,並且job_id=5的所有記錄
select*from Products where ProductIDin(4,5,6):查詢出Products表中ProductID為4,5,6的所有資訊
select*from Products where UnitPrice>10and UnitPrice<30 order by UnitPrice:查詢出Products表中10<UnitPrice<30的所有資訊,並按照UnitPrice的大小由小到大排序
select*from Products where UnitPrice between10 and 30 order by UnitPrice:上面的另外一種寫法
select * from Employees where FirstName like‘A%‘:查詢出Employees中FirstName裡面第一個字母是A的所有人資訊
select*from Employees where FirstName like‘%A%‘:查詢出Employees中FirstName裡面中間有A的所有人資訊
select*from Employees where FirstName like‘%A‘:查詢出Employees中FirstName裡面最後一個字母是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 欄位1=值1,欄位2=值2 …… 欄位n=值n where 條件運算式"
(3) 刪除資料記錄:
sql="delete from 資料表 where 條件運算式"
sql="delete from 資料表" (將資料表所有記錄刪除)
(4) 添加資料記錄:
sql="insert into 資料表 (欄位1,欄位2,欄位3 …) values (值1,值2,值3 …)"
sql="insert into 目標資料表 select 欄位名 from 來源資料表" (把來源資料表的記錄添加到目標資料表)
(5) 資料記錄統計函數:
AVG(欄位名) 得出一個表格欄平均值
COUNT(*¦欄位名) 對資料行數的統計或對某一欄有值的資料行數統計
MAX(欄位名) 取得一個表格欄最大的值
MIN(欄位名) 取得一個表格欄最小的值
SUM(欄位名) 把資料欄的值相加
引用以上函數的方法:
sql="select sum(欄位名) as 別名from 資料表 where 條件運算式"
set rs=conn.excute(sql)
用 rs("別名") 擷取統的計值,其它函數運用同上。
(5) 資料表的建立和刪除:
CREATE TABLE 資料表名稱(欄位1 類型1(長度),欄位2 類型2(長度) …… )
例:CREATE TABLE tab01(name varchar(50),datetime defaultnow())
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 更新資料表記錄
排序
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是升序。
SqlServer資料庫知識點筆記