orcl创建表及管理表

常用的字段数据类型:

.字符串(varchar2(n)) n表示保存最大长度,基本200作用。
.整数(number(n)) n位的整数,也可用int代替
.小数(number(n,m)) m为小数位,n-m为整数位,有时候用float代替
.日期(date) 存放日期
.大文本(clob) 存储海量文字(4G)
.大对象(blob) 存放二进制数据,电影,图片,文字,mp3

---------------------------------------------------------------------------------------------

1、创建表:

create table 表名称 (字段1 数据类型[default 默认值][primary key],...) 

create table bqh3 (name varchar2(20) not null, sfz number(18) ,dz varchar2(30),constraint pk_sfz primary key (sfz));

2、表数据及表的复制:

cerate table bqh (select * from 已知表)----从已知表中只复制数据

create table bqh as selec * from 已知表; -----从已知表中复制数据和表结构

create table bqh as select  字段1,字段2 from 已知表 where 1=2;----从已知表复制指定字段包含数据

create table bqh as select * from 已知表 where 1=2; ----从已知表中复制结构但不包含表数据

3、插入数据:

insert into 表名 [(字段1,字段2)] values (值1,值2)----字段与列一一对应

insert into bqh (name,sfz,dz) values ('小白',130684,'XX区1幢');

insert into 表名 values 所有列的值;--------可以省略字段

insert into bqh  values ('小白',130684,'XX区1幢');

4、更新数据:

update 表 set 列=新的值 ----更新所有的数据

update bqh set sfz=130636

update 表 set 列=新的值 [where 条件] -----更新满足条件的数据

update bqh set sfz=130636 where name='小白'

--------------------------------------------------------------

select * from 表 for update   ----手动更新的数据

select * from bqh  for update

select * from 表 for update where 条件

select * from bqh  for update where sfz=130636  -----手动更新满足条件的数据

5、删除数据:

delete from 表名称 [where 删除条件...]

delete from 表名 where 条件 -----删除满足条件的数据

delete from bqh where sfz=130636;

delete frombqh ----删除所有数据

commit; 

rollback; 

查看回收站:show recyclebin

恢复表:flashback table 表名称 to before drop

例如:flashback table bqh to before drop

恢复表数据:rollback

例如:delete from bqh;

           rollback;

注意:

①事物的回滚:ROLLBACK 更新操作回到原点。

②事物的提交:COMMIT 真正的更新,一旦提交无法回滚。

6、修改表结构:

alter table 表名称 add (列名称 数据类型 [default 默认值,...]) -----增加表字段

alter table bqh add (jf number(3) default 20)

alter table bqh modify (zf number(2) default 10) ------修改表字段

alter table bqh drop zf --------删除表字段

7、添加约束:

非空约束:not null不希望某字段为空

create table bqh (xh number,xm varcher2(20) not null)

唯一约束:unique 不希望某字段有重复值

create table bqh (xh number,xm varcher2(20) not null,email varchar2(50) unique)

指定唯一约束错误提醒:

create table bqh (xh number,xm varcher2(20) not null,email varchar2(50),constraint uk_email unique(email))

主键约束:primary key 非空约束+唯一约束例,如身份证

create table bqh (xm varcher2(20) not null,sfz number,constraint pk_sfz primary key (sfz))

检查约束:为某数据增加过滤条件,例如:年龄范围,性别等

create table bqh (xm varcher2(20) not null,xb varcher2(10) not null,age number(3),constraint ck_xb check (xb in ('男','女')),constraint ck_age check (age between 0 and 150))

外键约束:约束在两张(父子)表中进行,即字表某个字段的取值由父表决定。

例如:每个人有多本书,父表(人),子表(书)

create table xm (bh number,name varchar2(20) not null,constraint pk_bh primary key (bh))

create table book (bookname varchar2(20) not null,bh number,constraint fk_bh foreign key(bh) references xm(bh))

删除数据时,如果主表中的数据有对应子表数据,应先删除子表记录,再删主表记录。但操作不方便,即可以建立外键约束的时候指定一个级联删除功能,当删除主表数据后,对应的子表中的数据相关联的数据希望将其设置为空。

create table xm (bh number,name varchar2(20) not null,constraint pk_bh primary key (bh))

create table book (bookname varchar2(20) not null,bh number,constraint fk_bh foreign key(bh) references xm(bh)on delete set null)

8、修改约束:

为表增加约束

alter table 表名 add constraint 约束名称 约束类型(字段)

删除表中的约束

alter table 表名 drop constraint 约束名称

9、删除表:

drop table 表

drop table xm cascade constraint purge;  --------强制性删除表,不关心约束条件

drop table book cascade constraint purge; --------强制性删除表,不关心约束条件

感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接。

猜你喜欢

转载自www.cnblogs.com/su-root/p/9716278.html