赞
踩
数据字典:
user/dba_users 用户管理
user_contstraints 约束
dba_tables 表
all_tables 全部表
dba_tablespace 表空间
dba_data_files 表空间的文件
1. sys:权限都是比较高的,但是权限要比system高,而且登录的时候必须以管理员的身份进行登录。
2. system:权限最高,可以直接登录。
3. sysman:操作企业管理器来使用的,也是管理员级别的用户。
4. scott:
安装的时候,sys、system、sysman这三个是可以自己设定的,基本上都是统一的密码。
而scott统一密码都是tiger。
---------------
使用system用户登录:账号/密码 @orcl服务名 as sysdba || conn 账号/密码
select table_name from user_tables;
1. show user;查看登录用户
2. desc/describe dba_users 查看数据字典:是数据库提供的表,用户查看数据库的信息。(可以通过数据字典来查看其他的用户信息);
1. alter user username account unlock/lock加解锁
2. 根据目录找:oracle\product\11.20\server\rdbrus\admin\scott.sql;其次执行@$ORACLE_HOME/rdbms/admin/scott.sql
3. 修改密码:alter user scott identified by 密码
首先登录sqlplus,然后进行解锁;密码默认是tiger。
oracle的优化都是通过表空间来实现的
在数据库中开辟一个空间用来存储数据,数据库可以由多个表空间构成。
表空间是由一个或者多个数据文件来构成的,数据文件的位置和大小可以由我们自己来定义。
我们经常使用的数据、表都是存放在表空间的文件里面的。
表空间的分类:
1. 永久性表空间:表、视图、
2. 临时表空间:数据库操作当中执行的过程,执行完消失
3. UNDO表空间:存放一些被修改之前的数据,可以对数据进行回滚操作。
dba_tablespaces:系统查询的数据字典
system:存储sys用户的表、试图、 数据库对象 (系统表空间)
sysaux:example的辅助表空间
undotbs1:撤销信息
temp:sql语句处理的表和索引信息
users:永久性表空间,用户创建的数据库对象
example:安装oracle实例
user_tablespaces:用户查看的数据字典(记得切换scott用户)
与上述一样
dba_users:系统管理级别,可以查看的数据字典
user_users:普通用户,数据字典
查看默认表空间和临时表空间
select default_tablespace, temporary_tablespace from dba_users where username = "SYSTEM(大写)"
设置默认、临时表空间
alter user 用户 default/temporary tablespace 表空间名称
注:普通用户没有修改表空间的权限,必须是管理员级别。
创建表空间:
create temporary/tablespace 自定义表空间名称 tempfile/datafile '文件名称' size xx
查看:
文件的具体路径:
1. desc dba_data_files 数据字典
2. select file_name from dba_data_files where tablespace_name = '永久表空间名称(大写)'
3. select file_name from dba_temp_files where tempspace_name = '临时表空间';
当前用户所有表空间:
select tablespace_name from dba_tablespaces;
查看当前表所在的表空间:
select tablespace_name from all_tables where table_name = ''
查看表空间的所有表:
select table_name from dba_tables where tablespace_name = '表空间‘
设置联机、脱机状态:默认是联机状态(临时表空间不能修改)
alter tablespace 表空间名 online/offline;
设置只读、可写状态:
alter tablespace 表空间名 read only / read writer;
查看状态:
1. desc dba_tablespaces;
2. select status from dba_tablespace where tablespace_name = '表空间名称
增加数据文件:
alter tablespace 表空间名称 add datafile 'xx.dbf' size xx;
删除数据文件:
alter tablepsace 表空间名称 drop datafile 'xx.dbf'
我们删除表空间文件时候,不能删除第一个文件,如果想,可以删除整个表空间
如果删除表空间,也想把表空间的数据文件删除掉,那么可以使用:
drop tablespace 表空间名称 including contents
字符型:
1. char(n)-->max:2000、nchar(n)按照UICODE来存放数据的-->max:1000
2. varchar2(n)-->max:4000、nvarchar2(n)-->max::2000
数值型:
number(p, s)–> p: 有效数字、s:保留的小数点位数
float(n)–> 二进制数,如果想转为十进制,需要*0.30103
日期型:
date:精确到秒
timestramp:精确到小数秒,时间戳
其他类型:
blob: 可存放4G,以二进制
clob: 可存放4G,以文本型
创建表:
1. create table 表名(字段名 类型,字段名2 类型....)
2. 将表插入指定表空间:creat table 表名(字段铭 类型) tablespace test1tablespace;
查看表结构:
desc 表名称
查看表空间的表:
select table_name from dba_tables where tablespace_name = '表空间名称'
添加字段:
alter table 表名 add 列名 类型
更改字段数据类型:
alter table 表名 modify 字段 类型
删除字段:
alter table 表名 drop 字段名
更改字段名:
alter table 表名 rename column 旧字段 to 新字段名
修改表名:
rename 旧表名 to 新表名
添加数据:
insert into 表名 values()
添加数据的默认值:
1. insert into 表名 values(字段 类型 check(表达式 username >=3 ) , regdate date default sysdate)
2. alter table 表名 modify 字段 default 默认值
创建表复制:
create table 新表名 as select 字段 from 老表名
添加是复制:
insert into 表名(字段) select 字段 from 老表名(字段必须相同)
修改数据:
update 表名 set 字段=值
删除数据:
1. 属于数据定义语句,效率比2高:including contents
2. delete from 表名
添加:
1. create table 表名 (字段 类型 primary key, 字段 类型)
2. 在 create后面添加:constraint 主键名称 primary key(字段,)
3. alter table 表名 add constraint 主键名称 primary key(字段,)
忘记约束名字:user_contstraints 数据字典(约束)
select constraint_name from user_constrains where table_name = ’‘
删除:
1. 约束禁用:alter table 表名 disable constraint 约束名称
2. 查看约束:select status, constraints_name from user_constraints where table_name = ''
3. 删除约束:drop constraint 约束名称
4. 删除约束:drop primary key on delete cascade
添加:
1. create table 表名(字段 类型,外键 类型 references 主表名(字段))
2. create .. (字段,constraint 外键名成 foreign key(外键) references 主表名(主键列))
3. alter table 表名 add constraint ...
4. 主从表的数据类型要一致,外键的字段值来源于主表或者null
删除:
1. 禁用外键约束:alter table 表名 disable/enable constaraint 外键名称
2. 删除:alter table 表名 drop constraint 外键名称
添加:
1. create table 表名(字段 类型 not null)
2. alter table 表名 modify 字段 类型 not null
3. 再插入之前,表中不能有空值
删除:
alter table 表名 modify 字段 类型 null
添加:
1. create table 表名(列名 类型 unique, 列名 类型...)
2. create table 表名(列名 类型,constraint 外键名称 unique(字段) )
3. alter tbale 表名 add constraint 约束名称 unique(字段)
删除:
1. 禁用外键约束:alter table 表名 disable/enable constaraint 外键名称
2. 删除:alter table 表名 drop constraint 外键名称
作用:让值满足一定的条件,更有意义
检查约束:
1. create table 表名(字段 类型 check(表达式salary > 0) )
2. create table 表名(字段 类型,constraint 约束名称 check(条件))
3. alter table 表名 add constraint 约束名称 check(表达式)
删除检查约束:
1. 查看约束名称:select constraint_name, status, constraint_type from user_constraints where table_name = '表空间名称'
2. C代表:检查约束;P:外键约束
3. 禁用检查约束:alter table 表名 disable/enable constraint 约束名称
4. 删除:alter table 表名 drop constraint 约束名称(不区分大小写)
在创建表和修改表的时候,非空约束和其他不一致
更改约束名:非空是没有名称的,其他都可以更改。
根据数据字典:user_constraints
语法:rename constraint old_name to new_name
更改字段名称:
column 字段 heading new_name || 使用as
字段类型格式:
1. 数字:col/colmn 字段 format 99.9
2. 字段:col 字段 format a长度
3. 注:如果数值长度超过了设置的长度,那么将会用#代替
4. 还可以加$符号
清除格式:
col 字段名 clear
between…and
not in()
排序:
order by 字段1 desc降序,字段2 asc升序
分结果查询:
1. case..when:case 字段 when 值 then 结果,when 值 then 结果,else 结果 end
例子:select username, case username when 'aaa' then '研发部门' when 'bbb' then '销售部门' else '其他部门' end as '部门' from 表名
2. case..when:case when 字段=值 then 结果,when.. else 结果 end
例子:select username, case when username = 'aaa' then '研发部门',when username = ‘bbb’ then ‘市场部门’ else '其他部门' end as 部门 from 表名
3. decode函数:decode(字段1, 结果,字段2,结果, 结果)
例子:select username, decode(username='aaa', '研发部门',username='bbb', '销售部门', '其他部门') as '部门' from 表名
5.1 算术运算符(+ - * /)
5.2 比较运算符(> >= < <= <> =)
5.3 逻辑运算符(and or not)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。