Oracle基础

SQL plus的使用

    connect(连接)切换用户

    sys用户connect sys/java as sysdba 或者用sysoper

    show user 查看当前连接用户

    dba_users 数据字典 desc dba_users

    select username from dba_users 查看所有用户

解锁Scott用户:alter user scott identified by tiger account unlock;

             锁定:                 alert user scott account lock;

表空间的分类

1.永久表空间:存放表,视图,存储过程之类的东西。

2.临时表空间:数据库执行过程中存放的东西,在执行结束后会立即释放。

3.undo表空间:存放提交事务前的数据,给数据库提供回滚。


往表里面添加字段:alter table 表名 add 列名 数据类型

修改表里面的字段:alter table 表名modify 列名 数据类型

删除字段:alter table 表名 drop 列名 数据类型

修改字段名:alter table 表名 rename 老字段名 to 新字段名

修改表名:rename table to newtable

删除表: truncate table 表名(只删除表数据,不删除表结构,数据库定义语言效率高)

              delete from 表名(效率低)

              drop table 表名(删除表)

字段是date类型设置默认值default sysdate(系统时间)

向指定字段添加默认值:alter table 表名 modify 字段名 default 默认值

表的复制:create table new_table as select * from old_table(复制所有的字段)。

添加时复制:insert into 表1 select * from 表2(表2的数据复制到表1中)


非空约束

                添加:alter table 表名 modify 字段名 datatype not null;

                撤销:alter table 表名 modify 字段名 datatype null;

主键约束

                创建表时添加constraint 主键名 primary key(字段名,字段名)(联合主键)

                更改表时添加主键约束 alter table 表名 constraint add 约束名 约束类型(字段名)  

                更改主键约束的名字:alter table 表名 constraint rename 老约束名 to 新约束名。

                禁用主键约束 :alter table 表名 disable constraint 约束名

                删除主键约束:alter table 表名 drop constraint 约束名

                        alter table 表名 drop primary key

外键约束:在建表的时候添加       references 表名 (字段名)

                                                 constraint 外键名 foreign key (要参考的字段) references 被参考的表(被参考的字段)

                                                 on delete cascade//级联删除

                  添加外键:alter table 表名 add constraint 外键名 foreign key(字段)references 表名(字段)

                  禁用外键:alter table 表名 disable constraint 约束名

                  删除外键:alter table 表名 drop constraint 外键名。

唯一约束:添加约束:alter table 表名 add constraint 约束名 unique(字段)

                 禁用约束:alter table 表名 disable constraint 约束名

                 删除约束:alert table 表名 drop constraint约束名

检查约束:添加 constraint 约束名 check(条件)

                  alert table 表名 add constraint 约束名 check (条件)

                 禁用约束:alert table 表名 disable 约束名

                 删除约束:alert table 表名 drop 约束名

调整SQL plus样式       column(clo) 字段名 having newcolumn;//字段转化

                                   column(clo)字段名 format a10;//显示10位字符

                                   column (clo)字段名 format 99999.9999;//显示5位整数,四位小数。

                                   column (clo)字段名 clear;//清除所有格式。

--工资进行降序,如果工资相等就用id进行升序

select * from emp order by sal desc,empno asc
--去除重复------
select distinct deptno from emp
--in和or--
select * from emp where job in('CLERK','ANALYST') order by sal 
select * from emp where job='CLERK' OR JOB='ANALYST'ORDER BY SAL
--between...and   ....之间--
select * from emp where sal between 1500 and 5000 order by sal desc
--伪列--
select rownum ,empno from emp
--查询与Scott在同一个部门的所有员工
select * from emp where DEPTNO in (select deptno from emp where ename='SCOTT')
--查询员工工资高于部门平均工资的所有人
select * from emp e where sal>(select avg(sal) from emp e1 where e.deptno=e1.deptno) order by e.deptno
--笛卡尔乘积--
select * from emp,dept
--内连接查询--
select e.*,d.* from emp e inner join dept d on e.deptno=d.deptno ;
--左连接查询--
select e.*,d.* from emp e left outer join dept d on e.deptno=d.deptno ;
--右连接查询--
select e.*,d.* from emp e right outer join dept d on e.deptno=d.deptno;
--分页查询--

select * from (select rownum r,x.* from emp x where rownum<3)where r>1;

case...when和decode函数

    select ename,case ename when 'SMITH' then '张三' end as 姓名 from emp;

    select emp.*,case when ename='ALLEN' then '里斯' end as 姓名 from emp;

    select emp.*,decode(ename,'WARD','王五','SMITH','张三','ALLEN','里斯') as 姓名 from emp


猜你喜欢

转载自blog.csdn.net/qq_40529747/article/details/79792005