數據庫基礎筆記

group by用於對查詢結果分組統計sql

having子句用於限制分組顯示結果數據庫

自增加:identity1,1編程

建立彙集索引:服務器

create clustered index xxx(索引名稱) on xxx表名(xxx列名)oracle

建立非彙集索引:ide

create nonclustered index xxx(索引名稱)on xxx表名(xxx列名)函數

刪除索引測試

drop index 表名.索引名優化

Update text1(表名) set sal=sal*1.1 (後面能夠加where)編碼

外部候選人jane schaffer 的測試成績將加2

update xxx(表名)set sitestscore=sitestscort +2 where ccandidatecode=jane schaffer

Age float(2)將顯示8位數,包括小數(不夠8位全顯示,超過8位只顯示前8位)。

定義2位小數的numeric(10,2) 

刪除語句

主鍵能夠更改

`子查詢多表查詢分頁查詢等等。。

insert into 表名 字段列表

外鍵:

customerId nvarchar(50) foreign key references customer(customerId)

修改表中列的屬性:

alter table xxx(表名)

add constranint xxx (列名) +條件

create table dept( 

deptno int primary key,

dname nvarchar(30),

loc nvarchar(30)

)

select * from emp

select * from dept

create table emp2(

empno int primary key,

ename nvarchar(30), 

job nvarchar(30),

mgr int,

hiredate datetime, 

sal numeric(10,2),

comm numeric (10,2),

deptno int foreign key references dept(deptno)

)

 

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(,'','',,'',,)

insert into emp values(,'','',,'',,,)

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7369,'smith','clerk',7902,'1980-12-17',1600.00,20)

insert into emp values(7499,'allen','salesman',7698,'1981-2-20',1600.00,300,30)

insert into emp values(7521,'ward','salesman',7698,'1981-2-22',1250.00,500,30)

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7566,'jones','manager',7839,'1981-5-1',2975.00,20)

insert into emp values(7654,'martin','salesman',7698,'1981-9-28',1250.00,1400.00,30)

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7698,'blake','manager',7839,'1981-5-1',2850.00,30)

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7782,'clark','manager',7839,'1981-6-9',2450.00,10)

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7788,'scott','analyst',7566,'1987-4-19',3000.00,20)

insert into emp(empno,ename,job,hiredate,sal,deptno) values(7839,'king','president','1981-11-17',5000.00,10)

insert into emp values(7844,'turner','salesman',7698,'1981-9-8',1500.00,0,30)

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7876,'adams','clerk',7788,'1987-5-23',1100.00,20)

 

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7900,'james','clerk',7698,'1981-12-3',930.00,30)

 

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7902,'ford','analyst',7566,'1981-12-3',3000.00,20)

 

insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) values(7934,'miller','clerk',7782,'1982-1-23',1300.00,10)

insert into emp values(9999,'shunping','clerk',7782,'1988-5-5',2456.34,55.66,10)

 

insert into dept values(10,'accounting','new york')

insert into dept values(20,'resercher','dallas')

insert into dept values(30,'sales','chicago')

insert into dept values(40,'operations','boston')

 

 

 

查詢 smith的薪水工做和部門

select sal,job,deptno from emp where ename=xx

取消重複行的顯示:

select distinct ename,xx,xx from emp where XXXX

顯示人名和年工資

select ename,sal*13 '年工資' from emp order by sal

select ename '',(sal+comm)*13 '年工資' from emp order by sal

null換成0

select ename '',(sal+isnull(comm,0))*13 '年工資' from emp order by sal

入職時間在xxx之後的

select * from emp where hiredate>'1982-1-1'

工資在20002500之間的人

 

select * from emp where sal between 2000 and 2500

首字母是s的人

select ename from emp where ename like 's%'

第三個字母是m的人

select ename from emp where ename like '__m%'

顯示empno12345311、、、的僱員的狀況

select * from emp where empno in(10,20)

工資大於500或工做是manager,同時他們的姓名首字母必須是j的員工信息

select * from emp where (sal>500 or job='manager') and ename like 'j%'

升序,降序:

select * from emp order by hiredate desc, ename desc

顯示薪水最低員工的姓名和薪水

select ename,sal from emp where sal=(select min(sal) from emp)

顯示平均工資和總工資

select avg(sal)平均工資,sum(sal)總工資 from emp

把高於平均工資的員工的名字和他的工資顯示出來

select ename,sal from emp where sal>(select avg(sal)from emp)

統計共有多少員工

select count (*) from emp

select count(xxx)from emp

把高於平均工資的員工的名字和他的工資顯示出來並顯示平均工資

select ename,sal,(select avg(sal)from emp)平均工資 from emp where sal>(select avg(sal) from emp)

顯示每一個部門的平均工資

select avg(sal),deptno from emp group by deptno

如何顯示每一個部門的平均工資和最高工資

select max(sal),avg(sal),deptno from emp group by deptno

顯示每一個部門每種崗位的平均工資和最高工資

select max(sal),avg(sal),deptno from emp group by deptnojob 

顯示平均工資低於2000的部門號和他的平均工資

select avg(sal),deptno from emp group by deptno having avg(sal)<2000

 

複製信息:

create table test(

testId int primary key identity(1,1),

testName varchar(30),

testPass varchar(30),

)

insert into test(testName,testPass) values ('shunping','123321')

insert into test(testName,testPass) select testName,testPass from test

找出即買了a產品,又買了b產品,還買了c產品的客戶

select custom where

多表查詢高於部門平均工資的員工的信息

select emp.ename,emp.sal,tem.myavg,emp.deptno from emp,(不能用and

(select avg(sal) myavg,deptno from emp group by deptno)tem

where emp.sal>tem.myavg and emp.deptno=tem.deptno

分頁查詢

顯示第5到第10個入職的僱員:先顯示前4個入職的

select top 4 * from emp order by hiredate

而後顯示不在這4個裏的前6

select top 6 * from emp where empno not in 

(select top 4 empno from emp order by hiredate ) order by hiredate

注意:::括號裏 必須是empno 而不能是*

顯示第五到第九人的信息(薪水高低

select top 5* from emp where empno not in (

select top 4 empno from emp order by sal desc

) order by sal desc

 

刪除一張表中的重複記錄

select distinct * into #emp from emp

delete from emp

insert into emp select * from # emo

drop table #emp

把第一張表非重複記錄和第二張表的非重複記錄顯示出來

select distinct * from emp union all select*from #emp order by empno

把兩張表的非重複記錄顯示出來:

select distinct * from emp union  select*from #emp order by empno

左外鏈接右外鏈接

select w.ename,b.ename  from emp w,emp b where w.mgr=b.empno(內鏈接)

左外連:顯示沒有上級的人,和有上級的人的名字和上級的名字(左邊表的記錄所有顯示,若是沒有匹配用null)

select w.ename,b.ename from emp w left join emp b on w.mgr=b.empno

右外連:

select w.ename,b.ename from emp w left join emp b on w.mgr=b.empno

列出title表中標題id和標題名,條件是價格大於出版商id0736所出版的全部書的價格

select id,tltle from title where price >all(select price from title where id=0736)

列出titiles表中標題id和標題名,條件是大於出版商id0736所出版書的最低價格

select id,title from title where price>any(select price from tltle where id=0736)

列出title表中標題id和標題名,該出版商所在城市包含在做者表中的城市

select id,title form title where city=any(select city from authors)

存入新表

select * into b from a where id=xxx

 

惟一的:

testname varchar(30)  unique 能夠不給值,空也算unique

複合主鍵:

由多個列共同構成一個主鍵,不能爲空

都寫完後 primary keytestIdtestname

check

規範輸入值:

sal int check(sal>1000 and sal<2000)

定義時間:

mesDate datetime default getdate()不給時間則按默認時間

商品表:

create table goods(

goodsId nvarchar(50) primary key,

goodsName nvarchar(80) not null,

unitprice numeric(10,2) check (unitprice>0),

category nvarchar(3)check(category in('食物','日用品')),

provider nvarchar(50)

)

select *from goods

客戶表

create table customer(

customerId nvarchar(50) primary key,

customerName nvarchar(30) not null,

address nvarchar(100),

email nvarchar(100) unique,

sex nchar(1) check(sex in('',''))default '',

cardId nvarchar(18) not null

)

服務表:

create table purchase(

customerId nvarchar(50) foreign key references customer(customerId),

goodsId nvarchar(50) foreign key references goods(goodsId),

nums int check(nums>0 and nums<30)

)

修改表:

 

 

Sql語言包含4個部分:數據定義語言:CREATEDROPALTER

數據操做語言:INSERTUPDATEDELETE

數據查詢語言:SELECT

數據控制語言:GRANTREVOKECOMMITROLLBACK

 

表名能以

字母,_開頭,不能以數字開頭

長度不能超過128字符

不能使用aql server的保留字 (除非用[]包起來)

不能用如下字符 A-Z,a-z,0-9,$,#,-

字符類型:

Big5編碼:支持繁體

Iso-8859-1編碼:支持歐文字母

國標碼(gb2312):針對中國漢字

Gbk碼:支持更多的漢字

 

Unicode 編碼:用兩個字節表示一個字符(可爲英文可爲漢字)(適合漢字)

unicode編碼:一個字節表示一個字母,兩個字節表示一個漢字

Char 定長 最大8000字符(非unicode編碼)(查詢時速度快)

Char10a,前3個字符放「小a」,後6個空格補全(非unicode編碼)

Varchar10)變長 小韓,前4個字符放小韓,後面回收

Nchar 定長 最大4000字符(unicode編碼)

Nvarchar 變長(unicode編碼)

Ntext 可變長度unicode數據編碼最大長度爲230次方-1個字符

Text 可變長度非unicode數據編碼最大長度爲231次方-1個字符

Bit 範圍 01

Int 範圍-231次方到231次方-1

Bigint 範圍-263次方到263次方-1

Float 存放小數,不推薦使用

Numeric 小數(若是存放小數最好用numeric

Unicode編碼:用兩個字節表示一個字符(能夠是英文字母能夠是漢字)

unicode 編碼:用一個字節表示一個字母,兩個字節表示漢字

Datetime 表示日期(用函數getdate()讀取)

insert into stu values(getdate())

Timestamp 時間戳

Decimal31float型,3位有效,1位小數

Image 保存圖像(不多用)通常保存圖片在圖片服務器(要求:帶寬足夠大)

Binary 能夠存放視頻,但每每將視頻文件保存在文件服務器上,sql server中只保留文件路徑,存取效率高。

Primary key 主鍵,用於表示惟一的記錄(不能重複出現且不能爲空)(主鍵也能夠修改但不能改爲已存在的主鍵)

 

外鍵:名字 類型 foreign key references 表單名(主鍵)

Batch 批量查詢/批量增刪改查。

插入部分字段 insert into (表單名)(字段名) values ()(外鍵只能指向主鍵,數據類型要一致)

Order by(按順序排列)(默認升序排列)(通常放在最後)

Order by desc(按順序排列)(降序排列)

分頁查詢:用到子查詢(後面講)

Select語句優化的原則

儘量讓能減小結構集的條件寫在右面

Group by(用於對查詢的結果分組統計)

Having(限制分組顯示結果)(每每和group by 結合使用,能夠對分組查詢到的結果進行篩選)

多表查詢是指基於兩個和兩個以上的表或是視圖的查詢,在實際應用中,查詢單個表可能不能知足你的需求

自鏈接:把一個表看作兩個表     

外鏈接:(左外連接,右外鏈接)

子查詢:單行子查詢:右面的select語句返回單行

多行子查詢     

分頁查詢  :

請顯示第五個到第10個入職的僱員(按照時間的前後順序) 

左外連接:

左邊的表的記錄所有都要顯示,若是沒有匹配的記錄,就用空來填寫

Select w.ename,b.ename from emp w left join emp b on w.mgr=b.empno

約束:

用於確保數據庫數據知足特定的商業規則,在oracle中,約束包括:not null

unique(與主鍵同樣不能爲空,但unique能放null),primary key(外鍵數據必須在主表中存在或者爲null),foreign keycheck 5

行級定義和表級定義:

Default

 

 

Preparedstatement 能夠提升執行效率(預編譯)

能夠防止注入漏洞,可是要求用問號賦值的方法才能夠

Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);

Connection ct=DriverManager.getConnection(jdbc:odbc:mytest,sa,123321);

Statement sm=ct.creatStatement();

sm.excuteUpdate(insert into article values(‘’,’’,’’));//添加刪除修改

ResultSet rs=sm.excuteQuery(select *from dept)//結果集,resultset能夠理解爲錶行的結果集

Rs指向結果集的第一行的上一行,能夠循環取出

條件賦值語句

ps=ct.prepareStatement("select * from users0 where username=? and passwd=? " );

ps.setObject(1, username);

 ps.setObject(2, password)

SQL Server編程

經常使用如下術語:

批量,變量,打印消息,註解,控制流語句

使用變量存儲一些值:declare @定義名 屬性

declare @cahrge int    select @charge=max(列名)from 表名

相關文章
相關標籤/搜索