日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

[精]SQL最全基礎(chǔ)教程(有本事別看啊!)

 Levy_X 2018-10-11

SQL基礎(chǔ)教程

一、SQL簡(jiǎn)介

1:什么是SQL?

A:SQL指結(jié)構(gòu)化查詢(xún)語(yǔ)句    B:SQL使我們有能力訪問(wèn)數(shù)據(jù)庫(kù)     C:SQL是一種ANSI(美國(guó)國(guó)家標(biāo)準(zhǔn)化組織)的標(biāo)準(zhǔn)計(jì)算機(jī)語(yǔ)言

2:SQL能做什么?

*面向數(shù)據(jù)庫(kù)執(zhí)行查詢(xún)   *從數(shù)據(jù)庫(kù)中取出數(shù)據(jù)   *向數(shù)據(jù)庫(kù)插入新的記錄   

*更新數(shù)據(jù)庫(kù)中數(shù)據(jù)   *從數(shù)據(jù)庫(kù)刪除記錄   *創(chuàng)建數(shù)據(jù)庫(kù)   *創(chuàng)建表   

*創(chuàng)建存儲(chǔ)過(guò)程   *創(chuàng)建視圖   *設(shè)置表、存儲(chǔ)過(guò)程和視圖的權(quán)限

3:RDBMS

RDBMS是指關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)

RDBMS是SQL的基礎(chǔ),同樣也是所有現(xiàn)代數(shù)據(jù)庫(kù)系統(tǒng)的基礎(chǔ),如MS SQL Server、IBM DB2、Oracle、MySQL以及Microsoft Access

RDBMS中的數(shù)據(jù)存儲(chǔ)在被稱(chēng)為表的數(shù)據(jù)庫(kù)對(duì)象中

表是相關(guān)的數(shù)據(jù)項(xiàng)的集合,他由列和行組成。

二、SQL語(yǔ)法

注意:SQL對(duì)大小寫(xiě)不敏感!??!

1:SQL語(yǔ)句后面的分號(hào)

某些數(shù)據(jù)庫(kù)系統(tǒng)要求在每條SQL命令的末端使用分號(hào)。

分號(hào)是在數(shù)據(jù)庫(kù)系統(tǒng)中分隔每條SQL語(yǔ)句的標(biāo)準(zhǔn)方法,這樣就可以在服務(wù)器的相同請(qǐng)求中執(zhí)行一條以上的語(yǔ)句。

如果使用的是MS Access和SQL Server 2000,則不必在每條SQL語(yǔ)句之后使用分號(hào),不過(guò)某些數(shù)據(jù)庫(kù)要求必須使用分號(hào)。

2:SQL DML和DDL

可以把SQL分為兩個(gè)部分:數(shù)據(jù)操作語(yǔ)言(DML)和數(shù)據(jù)庫(kù)定義語(yǔ)言(DDL)

SQL(結(jié)構(gòu)化查詢(xún)語(yǔ)句)適用于執(zhí)行查詢(xún)的語(yǔ)法。但是SQL語(yǔ)言也包含用于更新、插入和刪除記錄的語(yǔ)法。查詢(xún)和更新構(gòu)成了SQL的DML部分:select、update、delete、insert into 。 數(shù)據(jù)庫(kù)定義語(yǔ)言(DDL)部分使我們有能力創(chuàng)建或刪除表格,我們也可以定義索引(鍵),規(guī)定表之間的連接,以及事假表間的約束:

Create database、alert database、create table、alert table、drop table、create index、drop index

三、Select

User表里面的數(shù)據(jù)如下

 

查詢(xún)user表里面的user_name字段和user_age字段的所有數(shù)據(jù)

Select user_name,user_age from user

 

查詢(xún)user表中所有的字段數(shù)據(jù),用 * 表示列的名稱(chēng)

Select * from user

 

四、Distinct

Distinct選取所有的值的時(shí)候不會(huì)出現(xiàn)重復(fù)的數(shù)據(jù)

用普通的查詢(xún),查詢(xún)所有

Select * from user

 

Select distinct user_name,user_age from user

注意:不能有user_id,因?yàn)閮蓚€(gè)Mary的user_id不一樣,加上就不算相同數(shù)據(jù)

 

五、Where

1:查詢(xún)user_id等于1 的數(shù)據(jù)

Select * from user where user_id = 1

 

2:查詢(xún)user_age大于等于12的數(shù)據(jù)

Select * from user where user_age >=12

 

3:查詢(xún)user_age不等于12的數(shù)據(jù)

Select * from user where user_age <> 12

 

六、AND 和 OR

And和or在where子語(yǔ)句中把兩個(gè)或多個(gè)條件結(jié)合起來(lái)。如果需要兩個(gè)條件都成立就是用and如果只需要其中一個(gè)條件成立就使用or

Select * from user where user_name = 'mary' and user_age = 12

需要注意的是SQL使用單引號(hào)來(lái)環(huán)繞文本值,如果是數(shù)值則不需要引號(hào)

 

Select * from user where user_name='mary' or user_age =13

 

結(jié)合and和or使用圓括號(hào)來(lái)組成復(fù)雜的表達(dá)式

Select * from user where (user_name = 'mary' and user_age = 12) or(user_age =13)

 

七、Order by

1:對(duì)指定列進(jìn)行升序排列

Select * from user order by user_name

 

2:按照user_id逆序排列

Select * from user order by user_id DESC

 

2:按照升序排列user_id逆序排列user_age

SELECT * FROM user order by user_id ASC,user_age DESC

 

3:按照升序排列user_id逆序排列user_age

SELECT * FROM user order by user_age DESC,user_id ASC

 

注意:前面的條件優(yōu)先級(jí)更高??!

八、Insert

User表

 

插入一行數(shù)據(jù) user_id為2 user_name為tom,user_age為12

注意:如果每一項(xiàng)都有插入的話(huà)就不需要在前面列出列名!!

Insert into user values(2,'tom',12)

新插入一行數(shù)據(jù),只要求user_name為eva

Insert into user(user_name) values('eva')

注意:因?yàn)镮D設(shè)置為自增,所以u(píng)ser_id不為null

 

九、Update

修改user_id為6的數(shù)據(jù)user_age為14

Update user set user_age=14 where user_id=6

 

修改user_id為1的數(shù)據(jù)user_name為ann,user_age為11

Update user set user_name='ann',user_age=11 where user_id=1

十、Delete

User表中的所有數(shù)據(jù)信息如下

 

刪除user_age為12的數(shù)據(jù)

Delete from user where user_age=12

 

刪除表中的所有數(shù)據(jù)

Delete from user

 

第二章 SQL高級(jí)教程

一、Top

Top子句用于返回要返回的記錄的數(shù)目,但并不是所有的數(shù)據(jù)庫(kù)都支持top子句

1:SQL Server

Select top 5 * from user

2:MySQL

Select * from user limit 5

3:Oracle

Select * from user where ROWNUM <= 5

二、Like

User表的初始數(shù)據(jù)如下

1:找出以li開(kāi)頭的數(shù)據(jù)

Select * from user where user_name like 'li%'

2:找出以ry結(jié)尾的數(shù)據(jù)

Select * from user where user_name like '%ry'

 

3:找出含有a的數(shù)據(jù)

Select * from user where user_name like '%a%'

 

4:找出第二個(gè)字母是a第四個(gè)字母是y的數(shù)據(jù)

Select * from user where user_name like '_a_y'

 

三、通配符

在搜索數(shù)據(jù)庫(kù)中的數(shù)據(jù)的時(shí)候SQL通配符可以替代一個(gè)或多個(gè)字符。SQL通配符必須與like運(yùn)算符一起使用

1: _ 替代一個(gè)字符

找出第二個(gè)字母是a第四個(gè)字母是y的數(shù)據(jù)

Select * from user where user_name like '_a_y'

 

2: % 替代一個(gè)或多個(gè)字符

找出以ry結(jié)尾的數(shù)據(jù)

Select * from user where user_name like '%ry'

 

3: [] 字符列中的任意一個(gè)單字符

找出以a或者l開(kāi)頭的數(shù)據(jù)

Select * from user where user_name like '[al]%'

找出不是a或者l開(kāi)頭的數(shù)據(jù)

Select * from user where user_name like '[!al]%'

四、In

只要數(shù)據(jù)滿(mǎn)足in里面的一個(gè)條件就可以了

找到user_age是12或者13的數(shù)據(jù)

Select * from user where user_age in (12,13)

 

找到user_name是Harry和Mary的數(shù)據(jù)

Select * from user where user_name IN ('mary','harry')

 

五、Between

選取兩個(gè)值之間的數(shù)據(jù)

查詢(xún)年齡在12和14之間的數(shù)據(jù)

Select * from user where user_age between 12 and 14

 

查詢(xún)字母在Alice和John之間的數(shù)據(jù)

Select * from user where user_name between 'alice' AND'john'

 

六、Aliases

指定別名

假設(shè)我們有兩個(gè)表分別是user和Room 。我們分別指定他們?yōu)閡和r。

1:不使用別名

Select room.room_name,user.user_name,user.user_age from user ,room  Where user.user_age=12 and room.room_id = 1

 

2:使用別名

使用別名的時(shí)候直接將別名跟在后面,不使用as也可以

Select r.room_name,u.user_name,u.user_age from user as u,room as r  Where u.user_age=12 and r.room_id = 1

 

七、Join

數(shù)據(jù)庫(kù)中的表可以通過(guò)鍵將彼此聯(lián)系起來(lái),主鍵是一個(gè)列,在這個(gè)列中的每一行的值都是唯一的,在表中,每個(gè)主鍵的值都是唯一的,這樣就可以在不重復(fù)每個(gè)表中的所有數(shù)據(jù)的情況下,把表間的數(shù)據(jù)交叉捆綁在一起。

以下為表user和表Room的數(shù)據(jù)

 

    

1:引用兩個(gè)表

找出在Room of boy相關(guān)聯(lián)的用戶(hù)信息

Select u.user_name,u.user_age,r.room_name from user as u,room as r 

Where u.room_id = r.room_id and r.room_name='room of boy'

 

2:使用關(guān)鍵字join來(lái)連接兩張表

Select u.user_name,u.user_age,r.room_name

from user as u

join room as r

on u.room_id = r.room_id and r.room_name='room of boy'

 

八、Inner join

Inner join 與 join 用法一致

Select u.user_name,u.user_age,r.room_name

from user as u

inner join room as r

on u.room_id = r.room_id and r.room_name='room of boy'

 

九、Left join

注意:左連接以左邊的表為主體,也就是說(shuō)會(huì)列出左邊的表中的所有的數(shù)據(jù),無(wú)論它是否滿(mǎn)足條件。

1:user在左邊

Select u.user_name,u.user_age,r.room_name

from user as u

Left join room as r

on u.room_id = r.room_id and r.room_name='room of boy'

 

2:Room在左邊

Select u.user_name,u.user_age,r.room_name

From room as r

Left join user as u

on u.room_id = r.room_id and r.room_name='room of boy'

 

十、Right join

注意:左連接以右邊的表為主體,也就是說(shuō)會(huì)列出左邊的表中的所有的數(shù)據(jù),無(wú)論它是否滿(mǎn)足條件。

1:Room在右邊

Select u.user_name,u.user_age,r.room_name

from user as u

Right join room as r

on u.room_id = r.room_id and r.room_name='room of boy'

 

2:user在右邊

Select u.user_name,u.user_age,r.room_name

from  room as r

Right join user as u

on u.room_id = r.room_id and r.room_name='room of boy'

 

十一、Full join

1:user在左邊

Select * from user Full join room

 

2:Room在左邊

Select * From room full join user

 

注意:SQL錯(cuò)誤碼1054表示沒(méi)有找到對(duì)應(yīng)的字段名;錯(cuò)誤碼1064表示用戶(hù)輸入的SQL語(yǔ)句有語(yǔ)法錯(cuò)誤

十二、Union

Union操作符用于合并兩個(gè)或者多個(gè)SELECT語(yǔ)句的結(jié)果集

請(qǐng)注意,UNION內(nèi)部的select語(yǔ)句必須擁有相同數(shù)量的列。列也必須擁有相同的數(shù)據(jù)類(lèi)型。同時(shí),每條select語(yǔ)句中的列的順序必須相同。

下面是Room表和color表的數(shù)據(jù)

     

Select room_name from room

Union

Select color_name from color

 

默認(rèn)的union選取不同的值,如果想要有相同的值出現(xiàn)就使用union all

Select room_name from room

Union all

Select color_name from color

 

十三、Create DB

創(chuàng)建數(shù)據(jù)庫(kù)mysqltest

Create database mysqltest

 

十四、Create table

Create table sqltest(

Id int,

Name varchar(45),

Age int,

Salary float,

Time Date,

)

 

 

十五、Constraints

SQL約束,用于限制加入表的數(shù)據(jù)的類(lèi)型

常見(jiàn)約束:not noll、unique、primary key、foreign key、check、default

十六、Not null

Not null 約束強(qiáng)制列不接受NULL值。Not null 約束強(qiáng)制字段始終包含值,這意味著,如果不向字段添加值,就無(wú)法插入新的字段或者更新記錄

用法,在字段后面加上 not null

 

十七、Unique

Unique約束唯一標(biāo)識(shí)數(shù)據(jù)庫(kù)中的每一條記錄。Primary key約束擁有自動(dòng)的unique約束。需要注意的是,每個(gè)表里面可以擁有多個(gè)unique約束,但只能有一個(gè)primary key約束

1:MySQL用法,unique(字段名)

 

2:SQL Server 、 Oracle 、 MS Access在字段后面加

 

3:命名約束使用constraint

 

4:已經(jīng)創(chuàng)建了表之后需要添加約束

ALTER TABLE sqltest ADD UNIQUE(Age)

 

5:給已經(jīng)創(chuàng)建了的表添加約束并命名

ALTER TABLE sqltest ADD constraint unique_name UNIQUE(Age,salary)

 

6:撤銷(xiāo)約束

MySQL

在沒(méi)有給約束命名的情況下(上面的age約束)直接使用字段名就可以了

ALTER TABLE sqltest DROP INDEX age

  刪除后   

在約束有名字的情況下,直接使用名字就可以了

ALTER table sqltest  drop index unique_name

   刪除后  

SQL Server 、 Oracle 、 MS Access

ALTER table 表名 drop constraint 約束名

 

十八、Primary key

Primary key約束唯一標(biāo)識(shí)數(shù)據(jù)庫(kù)表中的每一條記錄,組件必須包含唯一的值。組件列不能包含NULL值。每個(gè)表都應(yīng)該有一個(gè)主鍵,并且每一個(gè)表都只能有一個(gè)主鍵

1:在MySQL中的用法

 

2:在SQL Server 、 Oracle 和MS Access中的用法

 

3:為已經(jīng)創(chuàng)建成功的表創(chuàng)建primary key約束

Alter table sqltest add primary key(id)

 

4:為已經(jīng)創(chuàng)建成功的表添加主鍵約束,以及為多個(gè)列定義主鍵約束

Alter table sqltest add constraint pk_name primary key (id,name)

 

5:在MySQL中撤銷(xiāo)主鍵

ALTER  TABLE sqltest DROP PRIMARY KEY

   刪除后   

6:在SQL Server、Oracle、MS Access中撤銷(xiāo)主鍵

Alter table 表名 drop constraint 主鍵名

十九、Foreign key

所謂的外鍵,即一個(gè)表的外鍵指向另一個(gè)表的主鍵

User表

Room表

在user表里面room_id列指向Room表里面的id列。Room表里面的id列是主鍵,user表里面的room_id列是外鍵。外鍵約束用于預(yù)防破壞表之間的連接動(dòng)作,外鍵約束也能防止非法數(shù)據(jù)插入外鍵列,因?yàn)樗仨毷撬赶虻哪莻€(gè)表的值之一。

 

二十、Check

Check約束用于限制列中的值的范圍。如果對(duì)單一的列定義check約束,那么改了只允許特定的值。如果對(duì)一個(gè)表定義check約束,那么此約束會(huì)在特定的列中對(duì)值進(jìn)行限制。

 

為已經(jīng)創(chuàng)建成功的表添加check約束

ALTER TABLE USER ADD CHECK (age>10)

二十一、Default

Default約束用于向列宗插入默認(rèn)值。如果沒(méi)有規(guī)定其他值,那么就會(huì)將默認(rèn)值添加到所有的新紀(jì)錄。

用法:

 

當(dāng)表已經(jīng)存在的時(shí)候,添加默認(rèn)值

ALTER TABLE sqltest ALTER NAME SET DEFAULT 'tom'

 

撤銷(xiāo)默認(rèn)值

 

二十二、Create index

索引,你可以在表里面創(chuàng)建索引,一邊更加快速高效地查詢(xún)數(shù)據(jù)。用戶(hù)無(wú)法看見(jiàn)索引,他們只能被用來(lái)加速搜索、查詢(xún)。

注意:更新一個(gè)包含索引的表需要比更新一個(gè)沒(méi)有索引的表更多的時(shí)間,這是索引本身也需要更新,因此,理想的做法是僅僅在常常被搜索的列上面創(chuàng)建索引。

1:創(chuàng)建一個(gè)索引

CREATE INDEX index_name ON color (color_id )

 

2:創(chuàng)建一個(gè)獨(dú)一無(wú)二的索引,即兩行不能擁有相同的索引值。

CREATE UNIQUE INDEX book_index ON book (book_id)

 

3:如果索引不止一個(gè)列,你可以在括號(hào)中列出這些列的名稱(chēng),用逗號(hào)隔開(kāi)

CREATE INDEX index_bank ON bank (bank_id,bank_name)

 

二十三、Drop

通過(guò)使用DROP語(yǔ)句,可以刪掉索引、表和數(shù)據(jù)庫(kù)

1:刪除索引

Drop index index_name on color

 

刪除之后

 

2:刪除表

DROP TABLE colorcopy

  刪除之后   

3:清空表

TRUNCATE TABLE color

  刪除之后  

4:刪除數(shù)據(jù)庫(kù)

DROP DATABASE mysqltest

 刪除之后  

二十四、Alert

 

1:添加列

Alter table user add salary float

 

2:刪除列

Alter table user drop column room_id

 

二十五、Increment

定義主鍵自增

 

二十六、View

視圖,一種基于SQL語(yǔ)句的結(jié)果集可視化表。視圖包含行和列,就像一個(gè)真實(shí)的表。視圖中的字段來(lái)自一個(gè)或多個(gè)數(shù)據(jù)庫(kù)中的真實(shí)的表中的字段,我們可以向視圖添加SQL函數(shù)、where以及join語(yǔ)句,我們提交數(shù)據(jù),然后這些來(lái)自某個(gè)單一的表。需要注意的是,數(shù)據(jù)庫(kù)中的結(jié)構(gòu)和設(shè)計(jì)不會(huì)受到視圖的函數(shù)、where或join語(yǔ)句的影響

1:創(chuàng)建一個(gè)視圖,字段來(lái)自u(píng)ser表和Room表

CREATE VIEW view_test AS

SELECT user.user_name,user.user_age,room.room_name

FROM USER,room

WHERE user.user_age>10

2:查詢(xún)視圖

Select * from view_test

 

3:撤銷(xiāo)視圖

DROP VIEW view_test

二十七、Date

 

二十八、Nulls

默認(rèn)的,表的列可以存放NULL值。如果表里面的某個(gè)列是可選的,那么我們可以在不想改列添加值的情況下插入記錄或者更新記錄,這意味著該字段以NULL值保存。注意,NULL和0是不等價(jià)的,不能進(jìn)行比較。

 

1:查詢(xún)NULL值 

select * from user where salary is null

 

2:查詢(xún)非NULL值 

select * from user where salary  is not null

 

二十九、數(shù)據(jù)類(lèi)型

MySQL主要有三種類(lèi)型:文本、數(shù)字、日期

 

三十、服務(wù)器 

 

第三章 SQL函數(shù)

一、SQL functions

在SQL當(dāng)中,基本的函數(shù)類(lèi)型和種類(lèi)有若干種,函數(shù)的基本類(lèi)型是:

合計(jì)函數(shù)(Aggregate function)和 Scalar函數(shù)

Aggregate 函數(shù),函數(shù)操作面向一系列的值,并返回一個(gè)單一的值。

Scalar 函數(shù),操作面向某個(gè)單一的值,并返回基于輸入值的一個(gè)單一的值。

二、Avg()

求平均年齡

Select avg(user_age) from user

 

求大于平均年齡的用戶(hù)

Select * from user where user_age>(Select avg(user_age) from user)

 

三、Count()

返回列的值的數(shù)目(不包含NULL)

注意,可以使用as來(lái)給count()取一個(gè)別名

 

Select count(user_id) from user

 

Select count(salary) from user

 

返回值不同的有多少

Select count(distinct user_name) from user

 

查詢(xún)所有列

Select count(*) from user

 

四、Max()

返回最大值,NULL不包括在計(jì)算中

 

Select max(price) as max_price from commodity

 

五、Min()

返回最小值,NULL不包括在計(jì)算中

 

Select min(salary) poor_man from user

 

六、Sum()

返回該列值的總額

 

Select sum(salary) from user

 

七、Group By 

用于結(jié)合合計(jì)函數(shù),根據(jù)一個(gè)或多個(gè)列對(duì)結(jié)果集進(jìn)行分組

 

SELECT cname,SUM(price) FROM commodity GROUP BY cname

 

八、Having

在SQL中增加having子句的原因是where不能與合計(jì)函數(shù)一起使用。用法和where 一樣

SELECT cname,SUM(price) FROM commodity

GROUP BY cname

HAVING  SUM(price)>20

 

九、Ucase()

把函數(shù)字段的值轉(zhuǎn)化為大寫(xiě)

 

SELECT UCASE(user_name) FROM user

 

十、Lcase()

將函數(shù)字段轉(zhuǎn)化為小寫(xiě)

 

Select lcase(user_name) from user

 

十一、Mid()

從文本字段中提取字符

 

Select mid(user_name,2,2) from user

 

十二、Round()

Round函數(shù)把數(shù)值字段舍入為指定的小數(shù)位數(shù)

 

Select round(salary,2) from user

 

十三、Now()

返回當(dāng)前時(shí)間

SELECT NOW() FROM user

 

示例:

--查找emp表

select * from emp;

--查找emp表的sal

select a.SAL from emp a;

--查找emp表的ename

select a.ename from emp a;

--emp表的sal*10

select a.SAL*10 from emp a;

--emp表的sal的平均值

select avg(a.sal) from emp a;

--emp表的sal的總和

select sum(a.sal) from emp a;

--emp表的sal的max

select max(a.sal) from emp a;

--emp表的sal的min

select min(a.sal) from emp a;

--emp表中sal<1000的信息

select * from emp where sal<1000;

--ename中含有A的信息

select ename  from emp where ename like'%A%';

--emp中ename不含有A的信息

select * from emp where ename not like'%A%';

 

 

--查詢(xún)系統(tǒng)時(shí)間

select sysdate from dual;

--計(jì)算薪資小于5000的員工的工資總和

select sum(sal) from emp where sal<5000 ;

--計(jì)算工資不高于平均工資的工資總和

select sum(sal) from emp where sal<(select avg(sal) from emp);

--計(jì)算工資小于4000的員工的平均工資

select avg((select sum(sal) from emp where sal<4000)) from emp;

--查詢(xún)薪水低于100000的員工姓名和sal

select ename,sal from emp where  sal<100000;

--計(jì)算20號(hào)部門(mén)的員工的最高工資和最低工資

select max(sal),min(sal) from emp where deptno=20;

--查詢(xún)工資大于1000,并且是20號(hào)部門(mén)的員工的全部信息

select * from emp where sal>1000 and deptno=20;

--求最高工資的員工的員工姓名和部門(mén)名稱(chēng)

select ename,deptno,sal from emp where sal=(select max(sal) from emp);

--將員工薪水小于5000且部門(mén)平均薪水大于1000的部門(mén)標(biāo)號(hào)列出,按部門(mén)平均薪水降序排序

select deptno from emp where sal<5000  group by deptno having avg(sal) >1000;

select sal from emp order by sal desc;

Order by *** desc

 

--查找表emp

select * from emp ;

 

--根據(jù)用戶(hù)名“Smiths”查找他所在的部門(mén)

select deptno from emp where ename='SMITH';

 

--查詢(xún)每個(gè)部門(mén)的最高工資的人員名稱(chēng)

select e.ename,e.deptno,e.sal from (select deptno as did ,max(sal) as m from emp group by deptno )  s,emp e,dept d where e.sal=s.m and s.did=e.deptno and d.deptno=e.deptno;

 

--查詢(xún)“Jones”之后第一個(gè)進(jìn)入公司的人

select * from emp where hiredate=(select min(hiredate) from emp where hiredate>(select hiredate from emp where ename='JONES')) ;

 

--5.查找工資最高的部門(mén)名稱(chēng)和工資最低的部門(mén)名稱(chēng)及工資

select d.dname,e.sal from emp e,dept d where e.deptno=d.deptno and sal=(select max(m) from (select deptno,max(sal) as m from emp e group by deptno) s)

union

select d.dname,e.sal from emp e,dept d where e.deptno=d.deptno and sal=(select min(m) from (select deptno,min(sal) as m from emp e group by deptno) s)

 

 

 

--創(chuàng)建表

create table student(

       StudentId number (6),--學(xué)號(hào)

       LoginPwd varchar(20),--密碼

       StudentName varchar(50),--姓名

       Sex char(2),--性別

       Gradeld Number(6),--所在年級(jí)

       Phone number(15),--聯(lián)系電話(huà)

       Address varchar2(255),--現(xiàn)住址

       BornDate Date,--出生日期

       Emile varchar2(50)--電子郵件

);

--添加數(shù)據(jù)

insert into student values(1001,'123456','趙六','男',1507,120,'北京','23-5月-1995','@10422');

insert into student values(1002,'123456','王五','女',1507,110,'北京','23-5月-1995','@10422');

insert into student values(1003,'123456','張三','男',1507,120,'北京','23-5月-1995','@10422');

insert into student values(1004,'123456','李四','女',1507,110,'北京','23-5月-1995','@10422');

--提交

commit;

--查詢(xún)此表

select * from student;

--根據(jù)條件修改

 update student set studentname='孫七',loginpwd='666666' where studentid=1001;

 select * from student;

--根據(jù)條件刪除

delete from student where studentid=1002;

select * from student;

--增加字段

ALTER TABLE 表名稱(chēng) ADD(列名稱(chēng) 數(shù)據(jù)類(lèi)型 [DEFAULT 默認(rèn)值],列名稱(chēng) 數(shù)據(jù)類(lèi)型 [DEFAULT 默認(rèn)值],…)

--刪除此表

drop table student;

--B卷 創(chuàng)建表空間

create tablespace mytestspace datafile 'e:week3.dbf' size 2M;

--創(chuàng)建用戶(hù)

create user zhangsan identified by zhangsan default tablespace mytestspace

temporary tablespace temp;

 

--創(chuàng)建角色并授權(quán)

create role fang;

grant create table to fang;

grant fang to zhangsan;

grant dba to zhangsan;

 

--創(chuàng)建表

create table teacher (

tid number primary key,

tname varchar2(50),

tdate date,

address varchar2(100)

);

select * from teacher;

--創(chuàng)建序列

create sequence teachers_sequence minvalue 1 maxvalue 100 (最小值,最大值)cycle (循環(huán))increment by 1(步長(zhǎng)值) start with 1(從1開(kāi)始);

insert into teacher values(teachers_sequence.nextval,'小李','01-1月-1999','北京');

insert into teacher values(teachers_sequence.nextval,'小張',to_date('1982-1-1','yyyy-mm-dd'),'北京');

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約