初识数据库(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外键一同删除

posted @ 2016-08-24 15:24  奔跑的咖啡豆  阅读(3044)  评论(0编辑  收藏  举报