欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle创建表、修改表、获取表等使用命令

程序员文章站 2022-08-09 18:23:35
创建表 create table test01( id int not null, name varchar(8) not null, gender varchar2(2) not null, ag...

创建表

create table test01(

id int not null,

name varchar(8) not null,

gender varchar2(2) not null,

age int not null,

address varchar2(20) default ‘地址不详’ not null,

regdata date

);

修改表

alter table test01 add constraint s_id primary key;

alter table test01 add constraint ck_infos_gender check(gender=’男’ or gender=’女’)

alter table test01 add constraint ck_infos_age(age>=0 and age<=50)

alter table 表名 modify 字段名 default 默认值; //更改字段类型

alter table 表名 add 列名 字段类型; //增加字段类型

alter table 表名 drop column 字段名; //删除字段名

alter table 表名 rename column 列名 to 列名 //修改字段名

rename 表名 to 表名 //修改表名

删除表

truncate table 表名 //删除表中的所有数据,速度比delete快很多,截断表

delete from table 条件//

drop table 表名 //删除表

插入语句

insert into 表名(值1,值2) values(值1,值2);

修改语句

update 表名 set 字段=值 [修改条件]

查询语句

带条件的查询

where

模糊查询

like % _

范围查询

in

对查询结果进行排序

oeder by desc||asc

case…when

select username,case username when ‘aaa’ then ‘计算机部门’ when ‘bbb’ then ‘市场部门’ else ‘其他部门’ end as 部门 from users;

select username,case username=’aaa’ then ‘计算机部门’ when username=’bbb’ then ‘市场部门’ else ‘其他部门’ as 部门 from users;

运算符和表达式

算数运算符和比较运算符

distinct 去除多余的行

column 可以为字段设置别名 比如 column column_name heading new_name

decode 函数的使用 类似于case…when

select username,decode(username,’aaa’,’计算机部门’,’bbb’,’市场部门’,’其他’) as 部门 from users;

复制表

create table 表名 as 一个查询结果 //复制查询结果

insert into 表名 值 一个查询结果 //添加时查询

查看表信息

desc test01;

创建表空间

永久表空间

create tablespace test1_tablespace datafile ‘testfile.dbf’ size 10m;

临时表空间

create temporary tablespace temptest1_tablespace tempfile ‘tempfile.dbf’ size 10m;

desc dba_data_files;

select file_name from dba_data_files where tablespace_name=’test1_tablespace’;

约束

非空约束 |主键约束|外键约束|唯一约束|检查约束

not null |primary key|unique|check|

联合主键 constraint pk_id_username primary key(id,username);

查看数据字典 desc user_constraint

修改表时重命名 rename constraint a to b;

修改表删除约束

禁用约束 disable constraint 约束名字;

删除约束 drop constraint 约束名字; drop primary key;直接删除主键

外键约束

create table typeinfo(typeid varchar2(20) primary key, typename varchar2(20));

create table userinfo_f( id varchar2(10) primary key,username varchar2(20),typeid_new varchar2(10) references typeinfo(typeid));

insert into typeinfo values(1,1);

创建表时设置外键约束

constraint 名字 foregin

create table userinfo_f2 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new foreign key(typeid_new) references typeinfo(typeid));

create table userinfo_f3 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade);

外键约束包含

删除外键约束

禁用约束 disable constraint 约束名字;

删除约束 drop constraint 约束名字;

唯一约束 与主键区别 唯一约束可以有多个,只能有一个null

create table userinfo_u( id varchar2(20) primary key,username varchar2(20) unique,userpwd varchar2(20));

创建表时添加约束

constraint 约束名字 unique(列名);

修改表时添加唯一约束 add constraint 约束名字 unique(列名);

检查约束

create table userinfo_c( id varchar2(20) primary key,username varchar2(20), salary number(5,0) check(salary>50));

constraint ck_salary check(salary>50);

/* 获取表:*/

select table_name from user_tables; //当前用户的表

select table_name from all_tables; //所有用户的表

select table_name from dba_tables; //包括表

select table_name from dba_tables where owner=’zfxfzb’

/*