Oracle之表的相关操作

#添加字段

格式:

alter table table_name

add column_name datatype;

例子:

alter table userinfo

add remarks varchar2(500);

desc userinfo

 

#更改字段数据类型

格式:

alter table table_name  modify colum_name datatype;

例子:

alter table userinfo    modify remarks varchar2(400);

说明:修改remarks字段的默认长度。

 

例子:

alter table userinfo modify userpwd number(6,0);

 

#删除字段

格式

alter table userinfo drop column column_name;

例子:

alter table userinfo drop column remarks;

 

#修改字段名

格式:

alter table table_name rename column column_name to new_column_name;

例子:

alter table userinfo   rename column email       to new_email;

说明:这里是修改字段email为new_email

 

#修改表名

格式:

rename table_name to new_table_name;

例子:

rename userinfo   to new_userinfo;

 

#删除表

格式:

TRUNCATE TABLE table_name;

说明:删除表中数据,但不包含表结构,此时为空表

例子:

truncate table new_userinfo;

说明:表被截断,此时为空表

desc new_userinfo;

 

格式:

drop table table_name;

说明:

删除表的结构和表的内容,此时表完全不存在

例子:

drop table new_userinfo;

  

———————————————————————————————————————————————————————— 

 操作表中的数据

 

#添加数据:

insert语句:

格式:

insert into table_name

(column1 ,column2,…)

values(values1,values2);

 

操作实例:

向表中的所有字段添加值

insert into userinfo

value(1,’xxx’,’123’,’4325@126.com’,sysdate);

说明:因为向表中所有字段添加值,所以

不用指定字段,可以省略不写,若不是向所有字段的值赋予值,则要指定字段名,在赋值时是指定字段赋予值,未指定字段不用赋值,且最后不显示。

sysdate是指获取当前日期。 

insert into userinfo(id,username,userpwd)

values(2,’yyy’,’123’);

操作实例:

向表中的所有字段添加值。

create table userinfo1

(id number(6,0),

regdate date default sysdate);

说明:sysdate是一个默认的值,一个系统的当前时间,为regdate赋予值。

例子:

insert into userinfo1(id)

values(1);

另外如果你自己添加了值,就会按添加的

值显示,否则按默认值显示。

 

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

 

复制表数据

1.在建表时把其它的表的结构和内容复制过来。

格式:

create table table_new

as

select column1,…| * 

from table_old;

说明:…表示复制部分数据,*表示复制全部数据。

例子:

create table userinfo_new

as

select * from userinfo;

例子:

create table userinfo_new1

as

select id,username from userinfo;

说明:这里只是复制了userinfo的部分字段,而某些字段为空。

2.在添加时复制

格式:

insert into table_new [(column1,…]

select column1,…| * from table_old;

说明:在添加数据的时候复制其它表中的数据。 并且insert into后面的表是已经存在的。

例子:

insert into userinfo_new

select * from userinfo;

说明:在已经存在的表中的添加重复数据。

id name

1  ss

2  aa

3  cc

1  ss

2  aa

3  cc

 

insert into userinfo_new(id,username)

select id,username from userinfo;

id name

1  ss

2  aa

3  cc

1  ss

2  aa

3  cc

id name

1   ss

说明:添加部分字段的数据。 

找个环境,插个表数据再试试,这个可能

不太对。

 

在环境中去练习,另外,项目很重要。

成年人的世界没有那么多的童话,也没有那么多的逆袭。
原文地址:https://www.cnblogs.com/shijinglu2018/p/9851096.html