oracle 增删改查语法,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;          #复制不一样的表结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值