• 修改表和约束



        测试的表:
        create table t_user(
                id number constraint user_id_pk primary key,
                name varchar2(100),
                salary number
        );
        drop table t_user;

        //在表中添加一个新的列
        alter table t_user
        add birthday date;

        //删除表的某列
        alter table t_user
        drop column birthday;


        //给表中的列添加约束
        //这个约束相当于之前的表级约束
        alter table t_user
        add constraint user_name_un
        unique(name);
        
        //测试刚添加的唯一约束是否生效
        insert into t_user(id,name) values(1,'zs');
        insert into t_user(id,name) values(2,'zs');

        
        //删除表中的约束
        alter table t_user
        drop constraint user_name_un;


        //修改表的名字:
        rename t_user to mytest;
        rename mytest to t_user;

        //修改表中某列的类型
        alter table t_user
        modify (name varchar2(500));


        //让约束失效:必须知道约束的名字
        alter table t_user
        disable constraint user_id_pk cascade;
        
        //测试是否设置成功
        insert into t_user(id,name) values(1,'zs1');
        insert into t_user(id,name) values(1,'zs2');


        //让失效的约束再次生效
        alter table t_user
        enable constraint user_id_pk;


        //截断表中的数据(删除),不需要提交,默认已经提交,并且不能回滚
        truncate table t_user;
        相当于:
        delete from t_user;
        commit;


        //给表添加注释
        comment on table t_user is '很好';
        //给列添加注释
        comment on column t_user.name is 'good';
        //查看表中注释
        select * from user_tab_comments where table_name=upper('t_user');
        //查看列中的注释
        select * from user_col_comments
        where
        comments is not null
        and
        table_name=upper('t_user');



    第十一章: 序列
        Sequence 序列
        作用:帮我们生成主键列的值(特点:非空唯一)

        创建序列:
        一般不需要设置sequence的属性,使用默认的方式去创建就可以了.
        create sequence 序列名;

        如果需要设置属性,那么就加上下面的语句.
        [INCREMENT BY n]  每次拿出值加多少
        [START WITH n]    初始值从几开始
        [{MAXVALUE n | NOMAXVALUE}]  最大值
        [{MINVALUE n | NOMINVALUE}]  最小值
        [{CYCLE | NOCYCLE}]  到了最大值后是否循环(如果循环会从1开始)
        [{CACHE n | NOCACHE}] 每次在缓存里面放多少个值.


        例如:创建序列并设置属性
        create sequence seq_test
        increment by 2
        start with 45
        maxvalue 60
        cycle
        nocache;

        drop sequence seq_test;

        例如:创建序列使用默认属性
        create sequence seq_test;

        
        对应序列,我们只有俩种操作:
            1.获得序列中的下一个值
                //这个值对于当前这个序列来的其他值来说,肯定是非空唯一
                select seq_test.nextval
                from dual;

            2.查询序列中当前的值是多少
                select seq_test.currval
                from dual;

        
        向t_user表插入数据,其中id值可以需要生成
            create table t_user(
                id number constraint user_id_pk primary key,
                name varchar2(100),
                salary number
            );
            drop table t_user;
        
        //创建序列
        drop sequence t_user_seq;
        create sequence t_user_seq;
        
        //插入数据 使用序列产生id值
        insert into t_user(id,name,salary) values(t_user_seq.nextval,'tom',2000);
        

        通过数据字典查询当前用户的序列
            select sequence_name
            from user_sequences;

            select table_name
            from user_tables;

  • 相关阅读:
    对比使用Charles和Fiddler两个工具及利用Charles抓取https数据(App)
    Charles-安装和配置
    python算法-队列
    python算法-快速排序
    【Codeforces】383.DIV2
    static关键字
    UNIX环境高级编程--5
    【LeetCode】467. Unique Substrings in Wraparound String
    typedef关键字
    strcpy 和 memcpy自实现
  • 原文地址:https://www.cnblogs.com/yxj808/p/12023377.html
Copyright © 2020-2023  润新知