ORACLE SQL DML語句的簡單應用

2021-09-19 04:59:36 字數 2439 閱讀 4704

關鍵字:

insert

into

向**中插入資料

update

set更新資料語句

沒有where

語句更新全部資料

delete

刪除資料

以上的語句操作都可以回滾

插入資料的規則:

按列的預設順序列出各個列的值。

在 insert 子句中隨意列出列名和他們的值。

字元和日期型資料應包含在單引號中

insert

into語句 插入資料演示:

insert

into

employees(employee_id,last_name,email,hire_date,job_id)

values

('299','

雙擊','sf'

,to_date(

'2000-2-21'

,'yyyy-mm-dd'

),'ad_pres');

效果:

說明:如果受到約束時要按照其規則來插入資料比如資料型別為時間的要轉換成時間型別;插入資料的範圍不能超過**定義的範圍,否則查不進去。

加入子查詢時不用新增values

演示:insert

into

emp(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,commission_pct

,manager_id,department_id)

select

* from

employees

where

department_id in(

90,80);

就是查詢employees表部門號為80和90的員工將其插入到emp表中,如果插入列的與emp中的列的名稱和資料型別相同可以寫乙個*來代替插入資料的列;

update  更新資料語句

演示:update

emp

setsalary=

1450;

效果:

說明:將emp表中的salary列裡面的所以資料都更新為1450;更新時一樣受到列中的約束更新的資料型別不同或資料範圍超過會報錯.

加入子查詢加入的子查詢時要用括號()擴起來:

update

emp

setsalary=(

select

salary

from

emp

where

employee_id=

102)

where

employee_id=

101;

更新前:

更新後:

說明:將emp表中的employee_id

(員工id

)為102

的salary

(工資)更新

employee_id

等於101的員工;

資料更新後如果後悔相要修改回原本的資料可以進行回滾操作;

預設快捷鍵是shift+f10;

delete刪除資料

不加條件刪除該錶的所以資料:

delete

from

emp;

效果:加上where

條件:

delete

from

emp

where

last_name=

'partners';

使用子查詢:

delete

from

emp

where

salary= (

select

salary

from

emp

where

employee_id=

102);

使用子查詢可以刪除與員工102號相同的工資,當然也可以刪除基於另乙個表中的資料

刪除時如果其它表中的外來鍵連線到該錶的主鍵而且有外來鍵約束時就會造成刪除失敗;就是如果有員工和部門兩個表部門裡面有員工;如果想要刪除部門就要把部門裡面的員工刪除或轉移到其它的部門中去。

又稱為:刪除中的資料完整性錯誤。

delete不可以刪除**只能刪除裡面的行也就是資料.刪除一般是以行為單位來刪除的.

python的語句 Python的語句

python中的兩種語句 1 if條件控制語句 格式 if a int input 請輸入第乙個數 b int input 請輸入第二個數 if a b print a比b小 if else a int input 請輸入第乙個數 b int input 請輸入第二個數 if a b print a...

SQL 語句的TOP,Distinct語句

select top 3 from dbo.mystudent 查詢student表中前3條所有的資料 select top 3 s name,s gender,s address,s age from dbo.mystudent 查詢student表中前3條部分的資料 select top 3 p...

迴圈語句(for語句的用法)

for語句是最常用的迴圈語句,出現頻率極高,多用於各種迴圈計算。具體的形式如下 for 表示式1 表示式2 表示式3 表示式1 用於初始化變數,變數既可以是全域性也可以是區域性變數,區域性變數的作用域僅在for語句之內 表示式2 為判斷條件,當條件成立為真時 不等於0 執行迴圈,否則跳出 表示式3 ...