zoukankan      html  css  js  c++  java
  • oracle增删改查基本语法(DDL/DML)整理

    DDL:Data Definition Language(数据库定义语言)
    DML:Data Manipulation Language(数据库操作语言)

    一、表结构操作(create table、alter table、drop table)
      1.创建表
    create   table  tableName(

      id   number(6)   not   null   primary   key ,
      class_name  varchar2(8)   default   null,
      create_date  date
      );
    comment   on   column   tableName.id   is   'id';          #字段注释
    comment   on   column   tableName.class_name   is   '类名';
    comment   on   column   tableName.create_date   is   '创建日期';


      2.修改表结构
    alter   table   tableName   add   constraint   id    primary   key;        #增加约束,id设置为主键
    ----------------------------------------------------------------------------
    alter    table   表名   add   constraint   约束名   check(字段名='男'   or   字段名='女');        #增加约束,限制列的值(check为限制)

    示例1:给表tableName的gender字段增加约束,约束名为con_gender,gender限制为'男'或'女'
    alter    table    tableName    add   constraint   con_gender    check(gender='男'   or   gender='女');


    示例2:给表tableName的age字段增加约束,约束名为con_age,age限制为18至60之间'
    alter     table    tableName    add    constraint    con_age    check(age>=18   and   age<=60)
    -----------------------------------------------------------------------------
    alter    table    表名    modify    字段名及类型     default    默认值;          #更改字段类型

    示例:把表tb_user的字段username重新定义为number类型,最大长度为7,默认为0
    alter    table    tb_user    modify    username    number(7)    default 0;
    ---------------------------------------------------------------------------------------

    alter    table    表名    add   列名及字段类型;              #增加字段及类型

    示例:表tb_user新增一个height字段为number类型,最大长度为3
    alter    table   tb_user    add    height    number(3);
    -----------------------------------------------------------------------------------------
    alter    table     表名    drop    column       字段名;              #删除字段名
    alter    table     表名    drop    constraint    约束名;           #删除约束
    alter    table     表名     rename    column    列名    to    列名          #修改字段名
    rename    oldTableName    to  newOldTableName         #修改表名

      3.删除表
    drop    table   tableName;            #删除表,删除的包含表结构定义、表数据,并释放表占用的空间
    truncate    table     tableName;            #删除表数据,保留表结构定义及空间

    二、表数据操作(inser into、update、delete)
      1.插入表数据:insert  into
    insert   into   tableName(column1,column2,column3)  values(value1,value2,value3);
    #表列column1,column2,column3插入值value1,value2,value3

    insert   into   tableName   values(value1,value2,value3);
    #插入的数据与所有表列对应上时,可省略列名。

    insert   into   newTableName   select   *   from   oldTableName;
    #把老表中查出的数据插入到新表(新表与旧表表结构完全一致)

    insert   into   newTableName(column1,column2,column3)   select   value1,value2,value3   from   oldTableName;
    #把老表中查出的数据插入到新表(新表与旧表表结构可以不一致)

      2.修改表数据:update
    update   tableName   set   name='Mr Liu'   where   name='Ms Liu';       #把Mr Liu(刘先生)改成Ms Liu(刘女士)
    slect   *   from   tableName   for  update;                    #手动修改表数据
    slect   tb.rowid, tb.*  from   tableName  tb;                  #手动修改表数据

      3.删除表数据:delete from
    delete   from   tableName;                    #删除表所有数据
    delete    from    tableName  tb   where    tb.name='MrLiu';          #删除名字为MrLiu的数据

    三、复制表结构或者表数据
      1.复制表数据:
    create    table   newtable   as   select   *   from   oldtable;        #复制旧表结构及数据 
    insert     into   newtable   select   *    from   oldtable;           #已经创建了新表newtable结构,只复制数据
    注意:第一种方式只是复制了表结构,但是主键什么的并没有复制进去,所以用的时候要小心在意。

      2.复制表结构:
    create    table   newtable   as   select   *   from   oldtable   where   1=2;        #只复制完全一样的表结构

      3.复制表结构2:
    create   table   newtable   as   select  s.c1, s.c2   from  oldtable  s;          #复制不一样的表结构

  • 相关阅读:
    BUAA2020个人博客作业小结
    BUAA2020软工热身作业小结
    个人博客作业----总结
    个人阅读作业7
    超链接按钮点击变色,原来的链接恢复原色
    setInterval和setTimeout的区别以及setInterval越来越快问题的解决方法
    自定义网站404页面
    jQuery实现的上下滚动公告栏详细讲解
    K先生的博客
    Bootstrap4响应式布局之栅格系统
  • 原文地址:https://www.cnblogs.com/whitemouseV2-0/p/10877160.html
Copyright © 2011-2022 走看看