sql_constraint

来源:互联网 发布:淘宝快递破损怎么处理 编辑:程序博客网 时间:2024/04/30 04:14

约束种类
每一种约束的作用的了解演示。
  
   非空约束
   唯一性约束
   检查约束
   主键约束 --  每个表要有主键,唯一的标识一行数据,主键的特点非空并且唯一
   外键约束    外键引用的列一定是主键或有unique约束的列
  
     脚本(SCRIPT)
     列级约束并且非空约束(非空约束的特点是不能为null)
        create table cla( --班级表
          id int primary key, --班级编号
          cname varchar(20) not null --班级名字
       );
      
       alter table cla add age int default 20;
      
      
      
       唯一性约束
       create table clauni(
       id int,
       name varchar(20) unique,
       age int
       );
      
      
       insert into clauni(id,name,age) values(1,'zhang',20);
       insert into clauni(id,name,age) values(1,null,20);
       insert into clauni(id,name,age) values(1,'zhangs',20), (2,'lisi',20);
      
       
        表级约束的定义
        形式一:
        create table claunit(
        id int unique,
        name varchar(20),
        age int default 0,
        constraint claunit_name_uniquek unique(name)
        );
      
       insert into claunit(id,name,age) values(1,'zhang',30),(2,'zhang',30);
      
       形式二:
       create table claunit1(
        id int unique,
        name varchar(20),
        age int default 0,
        unique(name)
        );
       
        形式三:
       
        create table claunit2(
        id int unique,
        name varchar(20) unique,
        age int default 0
        );
      
       alter table claunit2 add constraint cla_age_uk unique(age);

约束字符

              创建带主键的,自动增长的id列
     create table depttest(
        id int primary key auto_increment,
        name varchar(255)
                     );
     create table jfw(
       id int not null unique,//列级约束
       name varchar(20),
       constraint jfw_name_uniq unique(name)//表级约束
                   );
 
   alter table jfww add job varchar(20) ;
   alter table jfww add constraint jfww_job_unik unique(job);//插入约束的另一种方式

      create table student(
        firstname varchar(20),
        lastname varchar(20),
        constraint student_f_l_prik primary key(firstname,lastname)
                     );//创建联合主键

  
      
      
      
       主键
       联合主键 表级定义
       create table student(
       firstname varchar(20),
       lastname  varchar(20),
       address   varchar(20),
       constraint student_prikey primary key(firstname,lastname)
       );
      
     insert into student (firstname,lastname,address) values('zh','wei','nj'),('zh','wei','bj');
     
     列级定义
     create table student1(
     id int primary key,
     name varchar(20)
     );

    insert into student1(id) values(1),(1);
      
      
       check 表达式
      
       create table student2(
       id int primary key,
       name varchar(20),
       age int check(age>18)
       )
      
      
 
       创建外键:
        create table stu(
            cid int,
            name varchar(20),
            age int
            )ENGINE=InnoDB;//子表必须为InnoDB型存储引擎

        create table class(
               cid int,
               banji carchar(20),
               manager varchar(20)
               )ENGINE=InnoDB;//主表必须为InnoDB型存储引擎
          alter table class modify id int primary key;//主表外键列名必须为主键。
          insert into class values(2,'test','chenzhen');
          insert into class values(1,'java','zhangbin');
          alter table stu add constraint stu_cid_fork foreign key(cid) references class(cid);//创建外键

          /*alter table class ENGINE=InnoDB;//修改存储引擎,创建外键后,主子表必须为InnoDB型存储引擎*/
      
       show create table stu /g; //显示表的详细信息:
        
       alter table empzw add constraint empzw_deptno_fk foreign key(deptno)  references dept(deptno);
                                                                                                          //创建外键
        
        删除外键
        alter table empzw drop foreign key empzw_deptno_fk ;
       创建新的外键
       alter table empzw add constraint empzw_deptno2_fk foreign key(deptno)  references deptzw(deptno)
       on delete cascade
       on update cascade;
      
       alter table empzw drop foreign key empzw_deptno1_fk ;
      
       alter table empzw add constraint empzw_deptno3_fk foreign key(deptno)  references dept(deptno)
       on delete cascade;

       alter table emptest add constraint empzw_dept_fk foreign key(deptno)  references deptzw(deptno)
       on delete cascade; 
        
       alter table empxh add constraint empxh_deptno_fk1 foreign key(deptno) references deptxh(deptno) on delete cascade;

      如何删除约束
  
  alter table student drop constraint  fk_stu;//没能成功!!
      可以用一个统一的格式来删除

  alter table 表名 drop constraint 约束名

   主键 = 非空 + 唯一
   非空
   唯一 = 有值的话  值要不同
   外键 = 有值 一定要在被引用的表的数据中
  
  
   复习:sql 语言包括的几部分:
   《1》DDL语句(数据定义语言) Data Define Language
   create
   alter
   drop
   truncate
   truncate table <表名>
    
 特点:<1>建立和修改数据对象
     <2>建立和修改直接存入库中,直接生效
    建立表
       create table class(--班级表
          classid number(2) primary key,
          cname varchar2(20) not null);
 
  alter table student add (shengfenzheng number(18));

  drop table student;  删除结构
  delete from student; 只删除数据,速度慢,数据可以恢复
  truncate table student; 删除记录的 速度快 数据不能恢复

 《2》 DML语句(数据操作语言) Data Manupilate Language    
   select
   insert
   delete
   update


 《3》 TCL(事务控制语句) Transaction Control Language


 
《4》 DCL语句(数据控制语句) Data Control Language                  

   select * from class,stu;
   select * from class,stu where stu.id=class.id;
   select c.id, c.banji,c.banzhu,s.name from class c,stu s where s.id=c.id;//c,s是别名,多表查询。
   select c.id, c.banji,c.banzhu,s.name from class c inner join stu s on  s.id=c.id;//内连接,作用同上。
   select c.id, c.banji,c.banzhu,s.name from class c left outer join stu s on  s.id=c.id;//左外连接,将连接的左边的表格中所有数据取出与右边匹配。
   select c.id, c.banji,c.banzhu,s.name from class c right outer join stu s on  s.id=c.id;//与上相反。
   select e.empno,e.ename,e.mgr, em.empno,em.ename from emp e,emp em where em.empno=e.mgr;//自连接。很强大!!
   select * from emp where sal > (select avg(sal) from emp);//子查询
   select d.ename as selfname,(select c.ename from emp c where c.empno=d.mgr) as maneger from emp d;//子查询!!