2022-06-16

1.三范式 : 行业标准

      最终的目的避免数据重复冗余,1NF-->列不可再分最小原子 (避免重复);2NF-->主键依赖(确

     定唯一);3NF-->消除传递依赖(建立主外键关联

拆分表);

表与表之间的关系:

           1)一对一关系: 用户表   身份证表  -->主外键关联关系

           2)一对多|多对一: 班级表  学生表    部门表与员工表  -->主外键关联关系

              --在多的一方设置外键字段关联一的一方的主键字段

           3)多对多: 订单与商品  -->中间表

2,--创建表+ 添加约束

     --默认约束名(表结构中,字段后面直接添加约束,默认约束名)  -->简单,不够灵活,报错提示不直观

 create tablestudent (

          --主键

         sidnumber(5) primary key,

          --非空

         snamevarchar2(5 char) not null,

         --性别  男或女

         genderchar(1 char) check(gender in ('男','女')),

         --入学日期  默认值sysdate

         hiredatedate default(sysdate),

         --手机号  唯一

         phone varchar2(11) unique,

         --所在班级编号  --外键

         cidnumber(5)

      );

(1)--字段后面指定约束名

       create tablestudent (

          --主键

         sidnumber(5) constraint pk_stu_id primary key,

          --非空

         snamevarchar2(5 char) constraint nn_student_name not null,

         --性别  男或女

         genderchar(1 char) constraint check_student_gender check(gender in ('男','女')),

         --入学日期  默认值sysdate

         hiredatedate default(sysdate),

         --手机号  唯一

         phonevarchar2(11) unique,

         --所在班级编号  --挖减

         cidnumber(5)

      );

(2)--表结构结束之前为某一个字段指定约束名添加约束

      create tablestudent (

          --主键

         sidnumber(5),

          --非空

         snamevarchar2(5 char),

         --性别  男或女

         genderchar(1 char) constraint check_student_gender check(gender in ('男','女')),

         --入学日期  默认值sysdate

         hiredatedate default(sysdate),

         --手机号  唯一

         phonevarchar2(11) unique,

         --所在班级编号  --挖减

         cidnumber(5),

         --为当前表中的字段添加约束

         constraintpk_stu_id primary key(sid),

         constraintnn_student_name check(sname is not null)

      );

(3),先创建表+ 追加约束   

      --为已有的表中的字段追加约束

       alter tableyjx_class add constraint pk_class_id primary key (cid);

(4),--默认约束名

         --cidnumber(5) references yjx_class(cid)

         --指定约束名1)直接在字段后指定约束名添加外键约束

         --cidnumber(5) constraint fk_stu_cid references yjx_class(cid)

         --指定约束名2)先声明字段,然后表结构结束之前为外键字段添加外键约束

         cidnumber(5)

        --constraint fk_stu_cid foreign key(cid) references yjx_class(cid)

      --指定约束名3) 表结构之间之后追加外键约束

      alter tablestudent add constraint fk_stu_cid foreign key(cid) references yjx_class(cid) ;

3,--加入注释

comment on table yjx_class is '班级表';

4,

--约束

       --物理约束: 真实在表字段上添加的约束

       --逻辑约束: 在代码上进行判断

--拷贝已有的表结构

--值拷贝结构不拷贝数据  <不能拷贝约束>

--create table 表名 as select字段列表from 已有表where 1!=1;

--拷贝结构同时拷贝数据

--create table 表名 as select字段列表from 已有表 where 条件;

5,

--删除表

--drop table 表名(cascadeconstraints)

--主外键关联关系下的两张表:

               --从表|子表   直接删除

               droptable student;

               --主表: 不能直接删除

                   --1)先删除所有的从表,再删除主表

                   --2)删除主表的同时级联删除主外键约束

                    drop table yjx_class cascadeconstraints;

                    drop table tb_user;

6,

--修改表结构

--修改表名

rename student to yjx_student;

7,--插入数据

insert into student(sid,sname,cid) 字段列表values(9001,'张三',207);

8,--约束的禁用与启用

ALTER TABLE yjx_class disable constraint pk_class_id;

ALTER TABLE yjx_class enable constraint pk_class_id;

--s删除约束

alter table yjx_class drop constraint pk_class_idcascade;

9,DML

拷贝

--以下两种可以实现在已有的表中拷贝数据插入新的表中 (一条|多条)

--insert into 表名 select 查询列from 源表 where 过滤数据;

--insert into 表(指定列)

select 查询列 from 源表 where 过滤数据;

10,

--update 表名 set 字段=值[,....] where 过滤行记录;

--30部门所有员工的薪资+1000

-update 表名 set (字段列表)

=(select 字段列表 from 源表 where 过滤源表记录) where 更新记录的条件手动更改字段值:

11,

--删除delete

--delete [from] 表名 where 过滤行记录

--删除表中所有的数据

delete from houhou_emp;

--删除表中的部门数据

delete from houhou_emp where sal<1500;

--删除主外键关联关系下的主从表中的数据   班级表  学生表

       --从表数据: 可以直接删除

       delete fromemp where empno = 9090;

       --主表中的数据:

                --删除的是主表中没有被从表引用的主表数据,可以直接删除

               delete from yjx_class where cid =207;

                --删除的是被从表引用的数据 : 不能直接删除,三种解决方案

                   --1)先删除所有从表中引用了当前要删除的主表数据的那些从表数据,然后再删除当前主表数据

                   --2)删除主表中数据的同时,级联删除从表中所引用了当前主表数据的那些从表数据 --> 在设置及外键约束的时候,  on delete cascade

                   --3)删除主表中数据的同时,从表中所引用了当前主表数据的那些从表数据外键字段设置为null -->在设置及外键约束的时候,  on delete setnull

              delete from student where cid = 207;

              delete from yjx_class where cid = 207;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • SQL分类根据SQL指令完成的数据库操作的不同,可以将SQL指令分为四类:* DDL Data Definitio...
    小铁和小钢阅读 644评论 0 0
  • 多表查询 笛卡尔积 虚拟表 s_emp s_dept 上述两个表所有的列在虚拟表都会出现, 引用那些列 原始表.列...
    冰川_阅读 472评论 0 0
  • 利用控制台命令连接 MySQL 启动 MySQL 服务net start 查看已经启动的服务net start ...
    梦想旅行家Jirry阅读 384评论 0 0
  • title: "Oracle创建表空间和表"date: 2021-02-01T20:17:20+08:00draf...
    大地缸阅读 256评论 0 0
  • Oracle笔记系列这几篇是来自一位老师的笔记,分享给大家放在简书上,主要方便自己时常复习,还有学习Oracle的...
    panda_Hi阅读 580评论 0 0