• oracle -- 表操作


    语法:

    1. CREATE TABLE schema_name.table_name (
    2. column_1 data_type column_constraint,
    3. column_2 data_type column_constraint,
    4. ...
    5. table_constraint
    6. );

    案例1:直接创建表,不设置约束

    1. CREATE TABLE tbl_students (
    2. stu_num CHAR(10) NOT NULL,
    3. stu_name VARCHAR2(10) NOT NULL,
    4. stu_sex CHAR(4) NOT NULL,
    5. stu_age NUMBER(3),
    6. stu_tel NUMBER(11)
    7. )
    8. // 给学生表新增一列
    9. ALTER TABLE tbl_students ADD stu_email VARCHAR2(20);
    10. // 新增
    11. ALTER TABLE tbl_students ADD stu_email VARCHAR2(20);
    12. // 修改列,仅支持修改类型和约束
    13. ALTER TABLE tbl_students MODIFY stu_email VARCHAR2(50)
    14. // 删除列
    15. ALTER TABLE tbl_students DROP COLUMN stu_email
    16. // 删除表
    17. DROP TABLE PERSONS;

    注:alter不能和本身冲突,比如性别保存男,3个字符,再修改会2个字符就会报错

    > ORA-01441: cannot decrease column length because some value is too big

    案例2:主键

    主键 数据表中的一个或多个字段,用于唯一表示数据表中的一条数据
    主键所在字段唯一且不为空,分为单列主键和多列主键

    单列主键:

    创建成功后,查看设计表时,可以看到键

    1. // 在创建表时创建主键
    2. CREATE TABLE tbl_students (
    3. stu_num CHAR(10) primary key,
    4. stu_name VARCHAR2(10) NOT NULL,
    5. stu_sex CHAR(2) NOT NULL,
    6. stu_age NUMBER(3),
    7. stu_tel NUMBER(11)
    8. )
    9. CREATE TABLE tbl_students (
    10. stu_num CHAR(10),
    11. stu_name VARCHAR2(10) NOT NULL,
    12. stu_sex CHAR(2) NOT NULL,
    13. stu_age NUMBER(3),
    14. stu_tel NUMBER(11),
    15. primary key(stu_num)
    16. )
    17. // 为列添加约束
    18. ALTER TABLE tbl_students ADD CONSTRAINTS pk_student primary key(stu_num)

     注:添加主键的修改不能和本身冲突,当表stu_num未标注为not null时,如果数据没有null,也是可以添加约束的,但是如果数据有null或者重复,则会报错,造成添加失败

    > ORA-01449: column contains NULL values; cannot alter to NOT NULL

    ORA-02437: cannot validate (WATERBOSSFACTORY.PK_STUDENT) - primary key violated(用重复的值)

    案例3:联合主键

    使用两个或两个以上的字段作为主键

    1. // 创建联合主键
    2. CREATE TABLE tbl_grads(
    3. course_id char(3),
    4. course_name VARCHAR2(50) ,
    5. stu_num CHAR(10),
    6. stu_name VARCHAR2(10),
    7. score NUMBER(3),
    8. primary key (course_id,stu_num)
    9. )
    10. // 下面的会报错, ORA-02260: table can have only one primary key
    11. CREATE TABLE tbl_grads(
    12. course_id char(3) primary key,
    13. course_name VARCHAR2(50) ,
    14. stu_num CHAR(10),
    15. stu_name VARCHAR2(10),
    16. score NUMBER(3) primary key
    17. )
    18. ALTER TABLE tbl_grads ADD CONSTRAINTS pk_grads primary key(course_id,stu_num)

    联合主键仅

     注:

    (1)只要有一个为空或者联合主键所在的字段都重复,则插入数据或者alter添加主键就会失败

    ORA-01449: column contains NULL values; cannot alter to NOT NULL

    > ORA-02437: cannot validate (WATERBOSSFACTORY.PK_GRADS) - primary key violated  两字段都重复

    案例4:外键约束

      主外键关联,即限定外键字段的值必须来自与其它数据表中的关联字段,一般是主键

    1. // 创建表时指定外键 如果外键关联的表不存在,则会报错 ORA-00942: table or view does not exist
    2. CREATE TABLE tbl_students(
    3. stu_num CHAR(10) primary key,
    4. stu_name VARCHAR2(10) NOT NULL,
    5. stu_sex CHAR(4) NOT NULL,
    6. stu_age NUMBER(3),
    7. stu_tel NUMBER(11),
    8. stu_cid CHAR(3) not NULL,
    9. CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade
    10. )
    11. // on delete cascade代表删除班级表的主键时,学生表的相关数据也会删除
    12. // 添加外键约束
    13. ALTER TABLE tbl_students ADD CONSTRAINTS fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade;
    14. // 删除外键约束
    15. ALTER TABLE TBL_STUDENTS DROP CONstraints fk_student_classes;

    注:

    (1)两个表的删除逻辑是必须先删除班级表,然后再删除学生表,因为学生表的外键指向了班级表的主键

    (2)设置了on delete cascade后,在删除班级表中的数据时,会同时删除掉班级的学生

    案例5:CHECK约束

    是检查约束,用于限定每一列能够输入的值,以保证数据的正确性

    1. // 添加check约束
    2. CREATE TABLE tbl_students(
    3. stu_num CHAR(10) primary key,
    4. stu_name VARCHAR2(10) NOT NULL,
    5. stu_sex CHAR(4) NOT NULL,
    6. stu_age NUMBER(3),
    7. stu_tel NUMBER(11),
    8. stu_cid CHAR(3) not NULL,
    9. CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade,
    10. CONSTRAINT ck_student_sex CHECK(stu_sex='男' or stu_sex='女'),
    11. CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
    12. )
    13. ALTER TABLE TBL_STUDENTS DROP constraints ck_student_age;
    14. ALTER TABLE tbl_students add constraints ck_student_age CHECK(stu_age BETWEEN 6 AND 30);

    添加好check约束后如下:

     此时添加数据不满足约束时,会报错

     

    案例6:UNIQUE约束

    用于限定字段的唯一性,唯一键添加方式有三种,一种是在字段直接添加unique,一种是使用CONSTRANT指定,这两种的区别是直接添加unique时唯一键的名称是随机的,CONSTRANT是指定的。

    1. // 电话是唯一的,添加唯一约束
    2. CREATE TABLE tbl_students(
    3. stu_num CHAR(10) primary key,
    4. stu_name VARCHAR2(10) NOT NULL,
    5. stu_sex CHAR(4) NOT NULL,
    6. stu_age NUMBER(3),
    7. stu_tel NUMBER(11) UNIQUE,
    8. stu_cid CHAR(3) not NULL ,
    9. CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade,
    10. CONSTRAINT ck_student_sex CHECK(stu_sex='男' or stu_sex='女'),
    11. CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
    12. )
    13. CREATE TABLE tbl_students(
    14. stu_num CHAR(10) primary key,
    15. stu_name VARCHAR2(10) NOT NULL,
    16. stu_sex CHAR(4) NOT NULL,
    17. stu_age NUMBER(3),
    18. stu_tel NUMBER(11),
    19. stu_cid CHAR(3) not NULL ,
    20. CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade,
    21. CONSTRAINT uq_student_tel UNIQUE(stu_tel),
    22. CONSTRAINT ck_student_sex CHECK(stu_sex='男' or stu_sex='女'),
    23. CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
    24. )
    25. ALTER TABLE TBL_STUDENTS DROP constraints uq_student_tel;

    添加唯一键后可以在唯一键的地方查看

    如果添加重复的数据,会报错

     案例7:NOT NULL

     已经一直使用,不再赘述

     

  • 相关阅读:
    实现微服务会带来哪些挑战?
    python 服务器数据转发数据 跳板机?
    Pair 和 Triple 应用实践/获取指定时间间隔
    认识操作系统 | 理解管理 | 系统调用(System Call)
    redis之GEO使用
    Real- Time Rendering-图形渲染管线(The graphics rendering pipeline)
    leetcode-151. 颠倒字符串中的单词-20220823
    Linux友人帐之日志与备份
    【C++ Primer Plus】第10章 对象和类
    针孔相机模型原理&坐标系辨析&内参标定流程&内参变换
  • 原文地址:https://blog.csdn.net/qq_45759288/article/details/125400721