您的位置:首页 > 数据库 > Oracle

oracle的一些SQL命令

2011-11-30 21:54 176 查看
第四章

1 逻辑备份:

(1)必备参数:exp system/abc123 file=d:/b.dmp;该命令并未指定登陆到哪个数据库实例,因此,将使用系统环境变量ORACLE_SID所指定的数据库实例。

(2)owner参数:exp system/abc123 owner=(test,oracle) file=d:/b.dmp;//指定仅仅导出test和oracle两个用户所拥有的对象。

(3)tables参数:exp system/abc123 tables=(people,employees) file=d:/b.dmp;//tables用于指定导出哪些数据表。

2 对于逻辑备份,相应的恢复命令为imp:imp system/abc123 files=d:/b.dmp;

实例:exp system/abc123@ORCL owner=(scott) file=d:/scott.dmp; //system/abc123@ORCL指定了欲进行备份的数据库的本地Net服务名及登陆用户名,密码;owner=(scott)指定了备份用户scott的所有对象;file=d:/scott.dmp指定了备份的目标文件。

第五章

1 创建名为user1的表空间:

create tablespace user1 datafile 'f:\database\user1_data.dbf' size 20M;

2 指定数据文件的大小可以自动扩展:

create tablespace user2 datafile:'f:\database\user2_data.dbf' size 20M autoextend on;

3 指定数据文件的增长幅度:

create tablespace user3 datafile 'f:\database\user3_dbf' size 20M autoextend on next 5M;

4 指定数据文件的最大尺寸:

create tablespace user4 datafile 'f:\database\user4_dbf' size 20M autoextend on next 5M Maxsize 500M;

5 查看表空间是否创建成功:

select file_name,tablespace_name from dba_data_files order by file_name;//视图dba_data_files存储了数据库中所有表空间的数据文件信息。其中tablespace_name列存储了表空间的名称;file_name则存储了对应表空间的数据文件的完整路径。

6 为一个表空间创建多个数据文件:

create tablespace multiple_data_file datafile 'f:\database\data_1.dbf' size 20M, 'f:\database\data_2.dbf' size 20M;

7 查看所有表空间信息: select tablespace_name,status,allocation_type from dba_tablespaces;

8 查询每个用户的默认表空间:select user_id,username,default_tablespace from dba_users;

9 修改数据库的默认表空间:alter database default tablespace user1;//alter database 是修改数据库属性命令,default tablespace user1则指定了新的表空间user1.该语句执行后,数据库的默认表空间被设定为user1。

10 修改表空间名称:alter tablespace user2 rename to user20;

11 删除表空间:drop tablespace user20;

12 删除表空间及其数据文件: drop tablespace user20 including contents and datafiles;

12 创建数据库表:create table t_user (user_id number not null,user_name varchar2(20) not null, user_email varchar2(30));

13 查看表结构: select table_name,tablespace_name from user_tables where table_name='t_user';

14 修改表结构:alter tablet t_user rename column user_email to email;

15 修改列属性: alter table t_user modify (user_name varchar2(15));

16 为表添加一列: alter table t_user add (remark varchar2(50));

17 删除表中的一列: alter table t_user drop column remark;

18 修改表名: alter table t_user rename to t_users;

19 删除数据表:drop table t_drop;

第六章:

1 所有用户创建的主键的基本信息都存储在表user_constraint表中。

查看主键约束:select table_name,constraint_name,constraint_type,status from user_constraints where table_name='student'; //查看表student的约束;

2 显示命名主键约束:create table student (student_id number constraint pk_student primary key, student_name varchar2(20),student_birthday date, student_address varchar2(50), student_phone varchar2(20));

3 创建主键的另一种写法:create table student (student_id number, student_name varchar2(20),student_birthday date, student_address varchar2(50), student_phone varchar2(20), constraint pk_student primary key(student_id));

4 为表添加主键:alter table student modify(student_id number primary key);

5 为表添加多列主键:alter table student add constraint pk_student primary key(student_name.student_birthday,student_address);

6 删除主键:alter table student drop primary key;

7 禁用表student的主键约束:alter table student disable primary kry;

启用表student的主键约束:alter table student enable primary key;

8 修改主键的名称:alter table student rename constraint SYS_C005145 to PK_STUDENT;//rename constraint用于修改约束的名称;

9 建立表orders到表customers的外键关联:alter table orders add constraint fk_orders_customers foreign key (customer_id ) references customers(customer_id);

10 为已存在的表添加唯一性约束:alter table users add constraint uq_phone unique (customer_phone);//uq_phone 是该唯一性约束的名称

11 删除唯一性约束:alter table users drop constraint uq_phone;

12 添加检查约束:alter table employees add constraint chk_name check(length(employee_name)<=4);

13 添加默认值约束:alter table sales modify quantity number default 1;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: