初識數據庫(TCL語句)


TCL語句 : 事物控制語句

  --什么是事物 : 多種操作能夠達到統一的結果

    --在網上購買了一部電話

    --1、查詢是否有該電話的庫存 ,將庫存 - 1

    --2、從銀行卡中扣錢,a)查詢卡中的錢是否足夠 , 2)在銀行卡中扣錢 , 3)將錢交給供貨方

    --3、已給物流、快遞

     -- 提交 / 回滾

事物控制的特性

  --1、一致性  2、原子性  3、持久性  4、統一操作(隔離性)

    一致性:指在一個事務執行之前和執行之后數據庫都必須處於一致性狀態。這種特性稱為事務的一致性。假如數據庫的狀態滿足所有的完整性約束,就說該數據庫是一致的。

    原子性:事務中包含的程序作為數據庫的邏輯工作單位,它所做的對數據改操作要全部執行,要么全部不執行。這種特性稱為原子性。

    隔離性:隔離性指並發的事務是相互隔離的。

    持久性:持久性意味着當系統或介質發生故障時,確保已提交事務的更新不能丟失。

DML語句:數據操作語言

  數據操作語言 -- DML語句都需要提交或回滾

  --增加  insert : 向表中插入數據   insert into 表名(列名1,列名2...) values(值,值...)

  --向部門表中插入一條數據,部門編號是50, 部門名稱 caiwu,部門所在地 beijing

    insert into dept(deptno,dname,loc) values(50,'caiwu','beijing')

    insert into dept values(60,'caiwu','beijing')

  --null如何插入空值

  --1、直接賦值

    insert into dept(deptno,dname,loc) values(50,'caiwu',null)

  --2、兩個單引號

    insert into dept(deptno,dname,loc) values(60,'caiwu',' ')

  --3、在指定列名時,忽略要插入空的列

    insert into dept(deptno,dname) values(60,'caiwu')

  --將自己的信息插入到員工表中

    select * from emp

    select * from dept

    insert into emp values (9527,'HUAAN','READER','7839','24-3月-2016',1000,200,null)

  --插入時間

    --隱式轉換

    --顯式轉換

  --利用通配符進行插入

    insert into dept (deptno,dname,loc) values(&a,'&b','&c')

  --同時插入多條數據(多行)

    create table emp1 as select * from emp where 1 = 2

    create table emp2 as select * from emp where 1 = 1

 

    select * from emp1

    select * from emp2

  --將emp表中部門號是30的員工插入到emp1中

    insert into emp1

    select * from emp where deptno = 30

  --update : 修改表中的數據  update 表名 set 列名1 = 值1,列名2 = 值2,... ...列名n = 值 n

  --where 條件必須要加

    update emp set ename = 'heheng'

    update emp set ename = 'heheng' where empno = 9527

  --給員工表中'SMITH'的工資上調200 

    update emp set sal = sal + 200 where ename = 'SMITH'

  --將'SMITH'調轉到'KING'的同部門下

    update emp set deptno =(

    select deptno from emp where ename = 'KING')

    where ename = 'SMITH'

  --將'HUAAN'調到'SCOTT'同部門下,並且將領導設置為'SCOTT'

    update emp set deptno =(

    select deptno from emp where ename = 'SCOTT')

    ,mgr =

    (select empno from emp where ename = 'SCOTT')

    where ename = 'HUAAN'

  --將員工的工資整體上調 2%

    update emp set sal = sal*1.02

  --修改'SMITH'同部門下的工資,按照10部門的平均工資+500修改,但是不包含'SMITH'

  --數字500要寫在表達式前面,子查詢寫在后面

    update emp set sal = (500+

    (select avg(sal) from emp where deptno = 10))

    where deptno =

    (select deptno from emp where ename ='SMITH')  and

    (ename <> 'SMITH')

  --delete : 刪除表中的數據   delete [from] 表名 [where] --限定刪除的數據

    --將'SMITH'同部門的,工資比他高的全部刪除

    delete from emp where empno in(

    select e.empno from emp e,(

    select deptno,sal from emp where ename = 'SMITH') hh                       

    where e.deptno = hh.deptno and e.sal > hh.sal)

DDL語句 : 數據定義語言

  --create 語句  :   create table 表名(列名1 數據類型[default 值],列名2 數據類型[default]... ...列名n 數據類型[default])

  --創建學生表

  create table student(

    --創建字段 學號  學號類型 數字型 長度為4位

      scode number(4),

    --創建姓名 字符型 長度是30位

      sname varchar(30),

    --創建生日

      sbirthday date,

    --創建身高

      sheight number(3),

    --創建性別

      ssex char,

    --創建班級

      cno number(2) default 1

      )

  --向學生表中插入一條數據

    select * from student

    insert into student values(9527,'華安','24-12月-1999',180,'B',default,75.21)

  --create table 表名 as 查詢

  --創建表內容是emp表中,部門號是10的部門

    create table emp3 as select * from emp where deptno = 20

    select * from emp3

  --創建表 只有 ename, sal , sal*12字段

    create table emp4 as select ename 姓名,sal 工資,sal*12 年薪 from emp

    select * from emp4

  --alter : 修改表屬性  -- 修改對象屬性

  --alter table 表名 rename column 原名 to new 新名

  --修改student表中 cno列名 為 sno

    alter table student rename column cno to sno

  --添加列

  --向學生表中添加體重列

    alter table student add (sweight number(3,1))

  --修改列屬性

    alter table student modify(sweight number(4,1))

  --添加默認值

  --添加

    alter table student modify sheight default (100)

  --刪除

    alter table student drop(sweight)

  思考 : 如果列中有數據 , 是否可以使用drop -- 沒有確認提交 ,慎重慎重

    alter table heihei drop(id)

    select * from heihei

  --刪除(銷毀)表

    --drop 刪除對象

    --drop table 表名

    drop table emp4

  --截斷

    --truncate : 截斷

  --與delete的區別  1、delete屬於DML語句 ,可以提交或回滾,truncate屬於DDL語句,直接對源數據進行操作

          --2、delete可以有條件,truncate沒有條件

          --3、是否釋放空間

          --4、從效率而言,truncate高於delete

  --語法結構   truncate table 表名

    truncate table student

    select * from student

添加約束

  在表的某一列上設定一個條件,防止無效的數據輸入該列

  --約束種類 :

    --1、not null : 非空約束,不允許為空

    --2、unique : 唯一鍵約束 ,該列中的值必須是唯一的,允許為空

    --3、primary key : 主鍵約束,唯一的、不能重復的、且不能為空的,它的值可以確定表中的某一行記錄

    --4、foreign key : 外鍵約束,保證表與表之間的數據完整性,兩個表A、B存在公共字段,

    --如果這兩個表的公共字段是A表的主鍵且是B表的外鍵,則A表和B表就可以關聯

    一般情況下,A表被稱為父表,B表被稱為子表

    --1、子表的公共字段里的值的范圍都是父表中的主鍵范圍

    --2、當父表里的某個值被子表里的外鍵所應用,此時不允許刪除父表中的對應行

    --3、check檢查約束  :  在某一列上設定一個布爾表達式

    delete from dept where deptno = 20

    drop table student

  --創建學生表,並為其中的字段添加約束

    create table student(

      --創建字段 學號  學號類型 數字型 長度為4位

      scode number(4) primary key,

      --創建姓名 字符型 長度是30位

      sname varchar(30) not null,

      --創建生日

      sbirthday date ,

      --創建身高

      sheight number(3) check(sheight>0),

      --創建性別

      ssex char(4),

      --創建班級

      cno number(2) default 1 references calss (cno)

      )

  --創建外鍵表calss

    create table calss(

    cno number(2) primary key,

    cname varchar(30) not null

    )

  --向學生表中插入一條數據

    select * from student

    select * from calss

    insert into student values(9527,'華安','24-12月-1999',180,'男',1)

    insert into calss values(1 ,'java')

    insert into calss values(2 ,'web')

    alter table student modify(ssex char(2))

    insert into student (scode,sname,ssex)values(9529,'華梅','女')

  --表級約束

  --創建學生表

  create table student(

    --創建字段 學號  學號類型 數字型 長度為4位

    scode number(4),

    --創建姓名 字符型 長度是30位

    sname varchar(30),

    --創建生日

    sbirthday date ,

    --創建身高

    sheight number(3),

    --創建性別

    ssex char(4),

    --創建班級

    cno number(2),

    --添加約束關鍵字--constraint

    constraint student_scode_pk primary key(scode),

    constraint student_sname_uk unique (sname),

    constraint student_sheight_ck check(sheight > 0),

    constraint student_cno_fk foreign key(cno) references calss(cno)

    )

  --給表里的列添加約束

  --創建學生表

    create table student(

      --創建字段 學號  學號類型 數字型 長度為4位

      scode number(4),

      --創建姓名 字符型 長度是30位

      sname varchar(30),

      --創建生日

      sbirthday date,

      --創建身高

      sheight number(3),

      --創建性別

      ssex char(2),

      --創建班級

      cno number(2)

      )

  --給表里的列添加約束

    select * from student

    select * from calss

  --給scode列添加主鍵約束

    alter table student add constraint student_scode_pk primary key(scode)

  --給sname列添加唯一約束

    alter table student add constraint student_sname_uk unique (sname)

刪除約束

  --級聯刪除  cascade 關鍵字

    alter table student drop constraint student_scode_pk cascade

  --把班級表的主鍵刪除,(有主外鍵關系的表)--測試級聯刪除的作用

    alter table calss add constraint calss_cno_pk primary key(cno)

    alter table student add constraint student_cno_fk foreign key(cno) references calss(cno)

    insert into student values(9527,'coco','24-3月-2016','175','男',1)

    alter table calss drop constraint calss_cno_pk  -- 此唯一/主鍵已被外鍵連接,刪除失敗

    alter table calss drop constraint calss_cno_pk cascade  --刪除成功,連帶student外鍵一同刪除


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM