首页 > 数据库技术 > 详细

ORACLE基本操作

时间:2018-06-14 00:20:32      阅读:194      评论:0      收藏:0      [点我收藏+]

1.创建table

create table test01(
       id int not null primary key,
       name varchar(8) not null,
       gender varchar2(2) not null,
       age int not null,
       address varchar2(20) default ‘地址不详’ not null,
       regdata date
);

约束

非空约束  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’

/*

 

2.修改表

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 表名 //修改表名

3.删除表格

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

delete from table 条件//

drop table 表名 //删除表

4.插入语句

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

5.修改语句

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

update t_scrm_db_app_user set password = :pwd where login_name = :user

6.查询语句

带条件的查询
    where

模糊查询
    like % _

范围查询
    in

对查询结果进行排序
    order by desc||asc

7.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;

8.运算符和表达式

算数运算符和比较运算符

  distinct 去除多余的行

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

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

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

9.复制表

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

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

10.查看表空间

desc test01;

 11.创建表空间

永久表空间
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’;

ORACLE基本操作

原文:https://www.cnblogs.com/xuhai/p/9180661.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!