复习数据库2

连接数据库

Conn scott/tiger@Orcl as Normal;

Disconn;

创建表空间

CREATE TABLESPACE DbDemo  DATAFILE 'DbDemo.dbf' size 400M autoextend on next 512M maxsize unlimited EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO

创建用户

-- Create the user

create user DbDemo

  identified by dbdemo

  default tablespace DBDEMO

  temporary tablespace TEMP

  profile DEFAULT;

-- Grant/Revoke role privileges

grant resource to DbDemo;

grant connect to DbDemo;

-- Grant/Revoke system privileges

grant unlimited tablespace to DbDemo;

 

修改用户的锁定

alter user DBDEMO  account lock;

-- Modify the user

alter user DBDEMO  account unlock;

修改用户密码

-- Modify the user

alter user DBDEMO  identified by "123";

创建表

-- Create DEPT table

create table DEPT

(

  deptno NUMBER(2) not null,

  dname  VARCHAR2(14),

  loc    VARCHAR2(13),

constraint PK_DEPT primary key (DEPTNO)

)

 

-- Create emp table

create table EMP

(

  empno    NUMBER(4) not null,

  ename    VARCHAR2(10),

  job      VARCHAR2(9),

  mgr      NUMBER(4),

  hiredate DATE,

  sal      NUMBER(7,2),

  comm     NUMBER(7,2),

  deptno   NUMBER(2),

constraint PK_EMP primary key (EMPNO)

)

 

alter table EMP  add constraint FK_DEPTNO foreign key (DEPTNO)

  references DEPT (DEPTNO);

 

增加记录

insert into emp (empno,ename,job,deptno) values(1233,'xiaoming','manager',20);

删除记录

delete from emp where empno=1233

更新记录

update emp set mgr=7092,ename='小明',Hiredate=to_date('19881027','yyyy/mm/dd') where empno=1234;

简单查询记录

select * from EMP t where job like 'CLERK';

关联查询

内连接

Select emp.empno,emp.ename,emp.job,dept.dname from emp,dept

Where emp.deptno=dept.deptno;

 

左外连接

Select emp.empno,emp.ename,emp.job,dept.dname from emp,dept

Where emp.deptno(+)=dept.deptno;

右外连接

Select emp.empno,emp.ename,emp.job,dept.dname from emp,dept

Where emp.deptno=dept.deptno(+);

子查询

在一个SQL语句中嵌套另一个SQL语句成为子查询。当一个问题无法通过一部解决时可考虑使用子查询,

一个出现在SELECT语句的FROM子句中的子查询被称为"内嵌视图";

一个出现在SELECT语句的WHERE子句中的子查询被称为"内嵌子查询",一个子查询能够包含另一个子查询,在一个顶级的查询中,Oracle数据库没有限制在FROM子句中的嵌套层数,可以在一个WHERE子句中嵌套255层子查询

posted @ 2013-09-12 17:13  太一吾鱼水  阅读(346)  评论(0编辑  收藏  举报