SQL基礎學習筆記(二)

來源:互聯網
上載者:User

標籤:


向表中插入一條資料

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>create table emp1as select employee_id , last_name , hire_date , salary from employeeswhere 1 = 2</strong></span>

需要注意的是,插入的資料,必須和表中資料種類,一一對應
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>insert into emp1values(1001,'abc',to_date('1998-12-11','yyyy-mm-dd'),10000)</strong></span>

如果salary是null
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>insert into emp1values(1002,'c',to_date('1999-01-11','yyyy-mm-dd'),null)注意,salary不寫,是不可以的</strong></span>

也可以是指定指定要輸入的值,注意(employee_id,last_name,hire_date),這個順序是可以改變的,但是value,
必須與(employee_id,last_name,hire_date)一一對應


<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>insert into emp1(employee_id,last_name,hire_date)values(1004,'c1',to_date('2999-05-11','yyyy-mm-dd'))</strong></span>

這樣salary就可以不寫了

有一點必須注意,只有可為空白的值,才能不賦值
Name        Type         Nullable Default Comments 
----------- ------------ -------- ------- -------- 
EMPLOYEE_ID NUMBER(6)    Y                         
LAST_NAME   VARCHAR2(25)                           
HIRE_DATE   DATE                                   
SALARY      NUMBER(8,2)  Y   

這個表last_name ,hire_date,就不可為空

從其他表中拷貝資料

--把employees表中80號部門的,下述資訊匯入新表
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>insert into emp1(employee_id , hire_date , last_name , salary)select employee_id  , hire_date , last_name , salaryfrom employeeswhere department_id = 80</strong></span>

建立指令碼(瞭解)

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>insert into emp1(employee_id , last_name , salary ,hire_date)values(&id,'&name',&salary,'&hire_date')</strong></span>



更新資料


<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>SQL> update emp1   2  set  3  salary = 12000  4  where employee_id = 1005;//注意不寫where會把所有的資料都改掉(commit;儲存)</strong></span>

如果不小心都改了,可以使用rollback;但是commit後的資料,是無法復原的

--更新114號員工的工作和工資使其與205員工相同
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>create table empployees1asselect * from employeesupdate empployees1set job_id = (             select job_id              from empployees1             where employee_id = 205              ),salary = (                          select salary                          from empployees1                         where employee_id = 205                         )where employee_id = 114;</strong></span>

--調整與employee_id 為200的員工job_id 相同的員工的department_id為employee_id為100的員工的department_id
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>update empployees1set department_id = (                      select department_id                      from empployees1                      where employee_id = 100                    )where job_id = (                    select job_id                    from empployees1                    where employee_id = 200               )</strong></span>

刪除資料

delete from...
where....

從emppployees1表中刪除部門名稱中含public字元的部門id

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>delete from empployees1where department_id = (                        select department_id                        from departments                        where department_name like '%Public%'                      )</strong></span>

注意delete操作的完整性錯誤

刪除一個含有員工的department_id,是不可以的,因為employees表不知道把這些人
怎麼辦

增:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>(1)insert into...values(...)(2)insert into... select ..from .. where ... </strong></span>

改:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>update ..set ..where ..</strong></span>

刪:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>delete from ..where ..</strong></span>

事務:

一組邏輯操作單元,使資料從一種狀態變換到另一種狀態。
資料庫事務由以下的部分組成:

一個或多個DML 語句
一個 DDL(Data Definition Language 資料定義語言 (Data Definition Language)) 語句
一個 DCL(Data Control Language  資料控制語言) 語句

以第一個 DML 語句的執行作為開始
以下面的其中之一作為結束:
COMMIT 或 ROLLBACK 語句


DDL 語句(自動認可)

rollback,復原的是最後一次的commit
提交或復原前的資料狀態
改變前的資料狀態是可以恢複的

執行 DML 操作的使用者可以通過 SELECT 語句查詢之前的修正
其他使用者不能看到目前使用者所做的改變,直到目前使用者結束事務。
DML語句所涉及到的行被鎖定, 其他使用者不能操作。
這個類似於多線程操作共用資料,當一個線程在操作改資料時,如果它不釋放鎖,其他線程是無法操作這個資料的

scott使用者正在update表,沒有commit
system使用者
select * from scott.employees for update;
就無法查詢,處於等待狀態,等到scott使用者commit了,就可以查詢了
這時scott使用者還想繼續修改表,就不行了,因為要等system使用者commit

SQL基礎學習筆記(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.