zoukankan      html  css  js  c++  java
  • Oracle建表


        建表的格式
            create table 表名(
                列名1 数据类型 列级约束,
                列名2 数据类型 列级约束,
                列名3 数据类型 列级约束,
                列名4 数据类型 列级约束
            );

            create table 表名(
                列名1 数据类型 列级约束,
                列名2 数据类型 列级约束,
                列名3 数据类型 列级约束,
                列名4 数据类型 列级约束,
                表级约束1,
                表级约束2
            );
        
        根据以上格式,可以看出,建表过程中,需要以下几种东西:
            1.关键字
            2.表名
            3.列名
            4.数据类型
            5.约束
            6.固定格式
        其中,约束分为列级约束(因为是跟在列的声明后面写的)和表级约束(因为是在全部列声明完之后写的),列级约束和表级约束都是对列中的值进行约束的,例如:列的值不能为空,列的值必须是唯一的等等
        注:列级约束也称为行级约束

        同时,列级约束和表级约束都是可选的,也就是都可以写也可以不写。例如:
            create table 表名(
                列名1 数据类型,
                列名2 数据类型,
                列名3 数据类型,
                列名4 数据类型
            );



        表名的要求:
            1.必须是字母开头
            2.必须是1-30个字符之间的长度
            3.表名中只能出现A–Z, a–z, 0–9, _, $, #
            4.不能和数据库中已有对象的名字重复
            5.不能是数据库中的关键字


        列的常用数据类型
            1.char
            2.varchar
            3.varchar2
            4.number
            5.date
            其他类型 BLOB CLOB
            BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的
        
        char varchar varchar2的区别及特点
            1.CHAR的长度是固定的,而VARCHAR2的长度是可以变化的
            2.CHAR的效率比VARCHAR2的效率稍高。
            3.VARCHAR是数据库标准的字符类型
            4. VARCHAR2是Oracle自己开发了一个数据类型VARCHAR2
            5. VARCHAR2将数据库中varchar列可以存储空字符串的特性改为存储NULL值。
            6. Oracle建议使用VARCHAR2而不是VARCHAR



        列的约束
            列的约束就是对这个列中的值的要求
            1.主键约束  PRIMARY KEY  primary key
            2.外键约束  FOREIGN KEY  foreign key
            3.唯一约束  UNIQUE       unique
            4.非空约束  NOT NULL     not null
            5.check约束 CHECK         check

            列级约束/行级约束:在列的后面直接加上的约束
            create table 表名(
                列名1 数据类型 列级约束,
                列名2 数据类型 列级约束,
                列名3 数据类型 列级约束,
                列名4 数据类型 列级约束
            );

            表级约束:在所有列声明以后加的约束
            create table 表名(
                列名1 数据类型 列级约束,
                列名2 数据类型 列级约束,
                列名3 数据类型 列级约束,
                列名4 数据类型 列级约束,
                表级约束1,
                表级约束2
            );


        建表实例
            例如1:
            //普通的建表例子
            create table student(
                id number primary key,
                name varchar2(200) not null,
                age number,
                birthday date
            );

            drop table student;

            
            例如2:
            //使用四种列级约束 主键约束 非空约束 唯一约束 check约束
            create table student(
                id number primary key,
                name varchar2(100) not null,
                email varchar2(100) unique,
                gender char(1) check(gender in('f','m')),
                age number,
                birthday date
            );
            
            drop table student;


            例如3:
            //使用列级约束 声明 外键约束
            create table t_customer(
                id number primary key,
                name varchar2(200) not null
            );
            
            create table t_order(
                id number primary key,
                content varchar2(200) not null,
                customer_id number references t_customer(id)
            );
            
            drop table t_customer;
            drop table t_order;

            注意:订单表中的外键列customer_id的值,是引用自顾客表t_customer中的主键列id的值
            1.这时候直接删除顾客表是不行的,因为t_customer的主键列的值被别的表给引用了.
            2.我们可以先删除订单表t_order,然后再删除t_customer就可以了
            3.如果非要想直接删除到顾客表t_customer,就需要使用下面的语句:
                drop table t_customer cascade constraints;
            4.该语句表示,删除t_customer表的同时,也级联删除与表相关的约束,外键约束没有了,这个表自然可以被删除掉
            5.cascade是级联的意思



            例如4:
            //使用表级约束
            create table student(
                id number,
                name varchar2(20) not null,
                age number default 20,
                email varchar2(100),
                gender char,
                primary key(id),
                unique(email),
                check(gender in('f','m'))
            );
            
            drop table student;

            注:非空约束(not null)不能声明成表级约束



            例如5:
            //使用表级约束 声明 外键约束
            create table t_customer(
                id number primary key,
                name varchar2(200) not null
            );
            
            create table t_order(
                id number primary key,
                content varchar2(200) not null,
                customer_id number,
                foreign key(customer_id) references t_customer(id)
            );
            
            drop table t_order;
            drop table t_customer;

            
            例如6:
            //使用表级约束 声明 联合唯一约束
            create table student(
                id number primary key,
                class varchar2(50) not null,
                name varchar2(50) not null,
                unique(class,name)
            );

            drop table student;
            
            注意:学生的班级和学生的名字联合起来必须是唯一的(联合唯一)
            注意:联合唯一约束必须使用表级约束来声明
            

            例如7:
            //使用表级约束 声明 联合主键
            create table t_customer(
                id number,
                name varchar2(50),
                primary key(id,name)
            );

            drop table t_customer;


            例如8:
            //使用表级约束 声明 联合外键
            create table t_customer(
                id number,
                name varchar(50),
                primary key(id,name)
            );

            create table t_order(
                id number,
                price number not null,
                customer_id number,
                customer_name varchar(50),
                foreign key(customer_id,customer_name) references t_customer(id,name)
            );


            drop table t_order;
            drop table t_customer;

            
            表级约束和列级约束对比
                1.表级约束和列级约束所写的位置不一样
                2.not null约束不能用表级约束来声明
                3.表级约束和列级约束声明语法稍有所不同
                4.如果要声明的约束为联合主键、联合外键、联合唯一的时候,就一定要用表级约束.


        constraint关键字
            1.constraint是约束的意思
            2.建表的时候可以给约束起一个名字,这个名字起的规律一般会是:表名_列名_约束类型
            3.如果没有给约束起名字,那么系统也会给这个约束起一个默认的名字,这不过这个默认的名字对我们来说并不友好(我们看不懂)
            4.将来我们可以根据我们之前给约束起好的名字而找到这个约束,然后进行修改

            例如1:
            //列级约束 起约束名字
            create table student(
                id number constraint student_id_pk primary key,
                name varchar2(100) constraint student_name_nn not null,
                email varchar2(100) constraint student_email_un unique,
                gender char(1) constraint student_gender_ck check(gender in('f','m')),
                age number,
                birthday date
            );
            
            drop table student;


            例如2:
            //表级约束 起约束名字
            create table t_customer(
                id number,
                name varchar2(20) not null,
                age number,
                email varchar2(100),
                gender char,
                constraint cus_id_pk primary key(id),
                constraint cus_email_un unique(email),
                constraint cus_gender_ck check(gender in('f','m'))
            );

            create table t_order(
                id number,
                price number not null,
                customer_id number,
                constraint order_cid_fk foreign key(customer_id) references t_customer(id)
            );
            
            drop table t_order;
            drop table t_customer;





        特殊的建表:建立一张表和s_dept一模一样
            例如1:
            //s_dept的表结构和表中的数据全部复制过来
            create table test1
            as
            select * from s_dept;
            
            例如2:
            //只拿来s_dept的表结构,没有数据
            create table test2
            as
            select * from s_dept
            where 1=2;
            
            例如3:
            //只复制表中某几个列以及数据
            create table test3
            as
            select id,last_name,salary from s_emp;


        ON DELETE CASCADE
        on delete cascade
            这个语句是在建表中,声明外键约束的时候一个可选项,我们后面的DML章节中再进一步讨论




     

  • 相关阅读:
    SQL Server的Execute As与连接池结合使用的测试
    为什么SQL语句Where 1=1 and在SQL Server中不影响性能
    [转]NGINX下配置CACHE-CONTROL
    ls列出当前目录包含子目录下面的所有文件的绝对路径
    [转]无法滚动到溢出容器的Flex项的顶部
    align-items和align-content的区别
    go实现快速排序
    [转]linux超级服务器inetd详解
    makefile 小记
    [转]gcc
  • 原文地址:https://www.cnblogs.com/yxj808/p/12023345.html
Copyright © 2011-2022 走看看