首页 技术 正文
技术 2022年11月18日
0 收藏 626 点赞 4,454 浏览 3121 个字

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;        #复制不一样的表结构

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731