Oracle创建表及管理表

匿名 (未验证) 提交于 2019-12-02 23:06:17

转自:https://www.linuxidc.com/Linux/2018-05/152566.htm

Oracle表的创建及管理

创建表包括三个要素,表名,列名,数据类型。每个表都有对应不同的列,每个列都有唯一对应的数据类型。常用数据类型简介:

数据类型
CHARACTER(n)
INTEGER(p)
NUMERIC(p,s)
DATE
TIMESTAMP

相关语句

--例:创建表名为table1,列名为column1,column2,…,数据类型为特定数据类型的表

Create table table1(
Column1 datetype,
Column2 datetype,
……
);

--添加字段:在已经建好的表table1中添加字段columnX,字符类型为number
Alter table table1
add columnX number;

--修改字段数据类型:修改columnX的数据类型为date
Alter table table1
Modify columnX date;

--修改字段名:修改columnX的名称为columnY
Alter table table1
Rename column columnX to columnY;

--删除字段:删除字段columnY
Alter table table1
Drop column columnY

--修改表名:修改表table1的名称为table2
Rename table1 to table2;

--删除表:删除表table2:
Drop table table2;
2.表中的数据管理

注:往表中添加数据时,字段数量与值得数量需一直并且一一按顺序匹配,添加的数据类型要符合表字段的数据类型

Insert into table2(column1,column2,……)
Values(value1,value2,……);

--第一种方法:在创建表时添加column1的默认值为0
Create table table1 (
Column1 number default 0;
Column2 datetype;
……
);

--第二种方法:创建好表后修改column的默认值为0
Create table table1 (
Column1 number;
Column2 datetype;
……
);

Alter table table1
Modify column1 default 0;

第一种方法:建表时复制,此时新建的table1与table2表结构相同

Create table table1
As
Select * from table2
注:可加入where字句限制限定插入数据

注:如只需要复制表结构而不需要数据,则加一不成立的条件即可:

Create table table1
As
Select * from table2
Where 1=2;
第二种方法:建表后复制,复制table2中的column11,column12两列数据至table1的column1,column2两列中

Insert into table1(column1,column2)
Select column11,column12 from table2
注:可加入where字句限制限定插入数据

修改表数据:修改表table1中column1的数据为value2

Update table1

Set column1=value2;
注:可加入where字句限制限定修改数据

删除数据:

第一种方法:有条件删除

Delete from table1

Where ……;
第二种方法:全部删除,删除table1中所有数据(不可回滚)

Truncate table table1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!