不同数据库的语法差距

  1. 创建表
    MySQL:
    create table test
    (id int(10) not null primary key comment '主键id',
    amt decimal(18,2) default null comment '金额'
    )
    comment='测试表';
    Oracle:
    create table t1(
    data_dt date,
    id varchar2(32) defaule 0 not null primary key,
    name varchar2(32) ,
    age varchar2(32)
    )
    COMPRESS PARTITION BY RANGE(data_dt)
    internal (numtodsinterval(1,'day'))
    (partition t1 values less than (to_date('2022-01-01','YYYY-MM-DD')));
    添加表注释:
    comment on table t1 is '个人信息';
    添加字段注释:
    comment on column t1.id is 'id';
    comment on column t1.name is '姓名';
    comment on column t1.age is '年龄';
  2. 删除表
    MySQL:drop table if exists tableName;
    Oracle:drop table tableName;
    注:Oracle没有if exists关键字,也没用类似if exists的SQL语法。
    drop、truncate、delete的区别:
    1、drop (删除表):删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表。
    drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。
    2、truncate (清空表中的数据):删除内容、释放空间但不删除定义(保留表的数据结构)。与drop不同的是,只是清空表数据而已。
    注意:truncate 不能删除行数据,要删就要把表清空。
    3、delete (删除表中的数据):delete 语句用于删除表中的行。delete语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存
    以便进行进行回滚操作。
    truncate与不带where的delete :只删除数据,而不删除表的结构(定义)
  3. 修改表名
    MySQL:
    A.rename table oldtableName to newtableName;
    B.alter table oldtableName rename to newtableName;
    Oracle:
    alter table oldtableName rename to newtableName;
  4. 修改表注释
    MySQL:alter table oldtableName comment '新注释'
    Oracle:comment on table 表名 is '表的注释信息';
  5. 添加字段
    MySQL:
    A. alter table tableName add [column] columnName1 int(10);
    B. alter table tableName add [column] columnName1 int(10), add column columnName2 int(10);
    Oracle:
    A.alter table tableName add columnName1 int;
    B.alter table tableName add (columnName1 int);
    C.alter table tableName add (columnName1 int, columnName2 int);
    注:对于添加多列时需要使用C,不能像MySQL那样重复使用add column关键字。
    在指定位置插入新字段:
    MySQL:
    A.alter table tableName add [column] columnName 数据类型 comment '注释' after 指定某字段;
    B.alter table tableName add [column] columnName 数据类型 comment '注释' first
    没有before的用法,只有first
    Oracle:无这样的用法,只能插在最后。
  6. 删除字段
    • 删除一个字段
      alter table tableName drop column columnName;
    • 删除多个字段
      alter table tableName drop (columnName1,columnName2);
  7. 修改字段名
    MySQL:alter table tableName change [column] oldcolumnName newcolumnName 新数据类型(必须);
    Oracle:alter table tableName rename column oldcolumnName to newcolumnName; 注:不能有字段类型
  8. 修改字段的注释
    MySQL:alter table tableName modify column columnName 数据类型 comment '修改后的字段注释';
    Oracle:comment on column 表名.字段名 is '字段的注释信息';
  9. 修改字段类型
    MySQL:alter table tableName modify [column] columnName 新数据类型 新默认值 新注释;
    Oracle:不允许修改字段类型
  10. 插入数据
    MySQL:
    insert into tableName (column1,column2,column3) values (null,column2value,column3value)
    Oracle:
    insert into tableName (column1,column2,column3) values (tableName.NEXTVAL,column2value,column3value)
    注:column1是自增主键字段,默认Oracle已创建了tableName名的序列序列参考:
posted @ 2021-04-15 13:58  蔓越煤  阅读(746)  评论(0编辑  收藏  举报