登录用户
connect
sys/Ljh @sever as sysdba/ sysoper
查看当前用户
Show user
查看所有用户
Desc dba_users
SQL语句:
Select username from dba_users;
锁定/解锁用户:
Alter user scott account unlock/lock;
什么是表空间?
表空间是存储各种表的空间.
表空间分为:
①永久表空间: 用于存放永久的表
②临时表空间: 用于存放数据操作过程中临时产生的表
③UNDO表空间:用于存放数据修改之前的原数据
查看表空间:
Desc dba_tablespaces / user_tablespaces
Select tablespace_name from dba_tablespaces;
查看用户:
Dba_users user_users
设置用户的默认/临时表空间:
Alter user system
Default / temporary tablespace system;
创建永久表空间:
Create tablespace test1_tablespace
Datafile ‘xxxx.dbf’ size 10m;
创建临时表空间:
Create temporary tablespace temp_test1_tablespace
Tempfile ‘xxx.dbf’ size 10m;
查看表空间文件的存取路径:
Desc dba_date_files / dba_temp_files
Select file_name from dba_temp_files where tablespace_name =’TEMP_TEST1_TABLESPACE’
修改表空间的联机/脱机状态:
Alter tablespace test1_tablespace
Online / offline
如何查看表空间的状态信息:
Desc dba_tablespaces
Select status from dba_tablespaces where tablespace_name=’TEST1_TABLESPACE’;
设置表空间的只读/读写状态:
Alter tablespace test1_tablespace
Read only / read write;
无论是只读状态,还是读写状态 都有个前提条件就是 表空间必须是联机状态. 若是脱机状态,则无论只读或是读写 都不起作用.
在表空间中 添加 / 删除 数据文件
Alter tablespace test1_tablespace
Add datafile ‘xxxx.dbf’ size 10m;
Alter tablespace test1_tablespace
Drop datafile ‘xxx.dbf’;
删除表空间
Drop tablespace test1_tablespace (只删除表空间,不删除数据文件)
Drop tablespace test1_tablespace including contents;
用户 表空间 数据文件:
用户登录:
锁定/解锁用户:
查看当前用户
查看所有用户:
设置用户的默认/临时表空间:
新建表空间:
删除表空间:
表空间中添加/删除数据文件:
查看表空间的联机/脱机状态:
查看表空间的读写/只读状态:
查看表空间的数据文件存取位置:
Dba_users user_users
Dba_tablespaces dba_data_files
二阶段开始:
什么是表?
二维表是存储的基本单位,,,属于 表空间.
数据类型:
字符类型:
固定长度: char(n): n的最大值为2K
Nchar(n): 支持unicode, 多用于汉字, n的最大值为 1K
可变长度: varchar2(n): n 的最大长度为4K
Nvarchar2(n): n的最大长度为2K
数值型:
Number(p,s)
Number(5,2) 5个有效数字 2位小数
Float(n) 存储二进制数字
日期类型:
Date :精确到秒 最大为公元9999年12月31日
Timestamp : 精确到 小数秒
其他类型:
Blob : 可以存放4G 的数据 ,二进制
Clob: 可以存放4 G 的数据, 字符串
创建一个表:
Create table 表名字
(
属性名 属性类型,
属性名 属性类型,
.....
);
Desc 表的名字
表中添加列:
Alter table userinfo
Add remarks varchar2(40);
表中修改列的类型:
Alter table userinfo
Modify remarks varchar2(30);
表中删除字段:
Alter table userinfo
Drop column remarks;
修改列名
Alter table userinfo
Rename column name to new_name;
修改表名:
Rename table_name to new_table_name;
删除表:
Truncate table new_userinfo;
该语句截断表,即是清空表的数据,但是表的结构保留.
Drop table new_userinfo;
该语句删除表的结构和数据.
往表中添加数据:
Insert into userinfo(column_name,column_name2.......)
Values (xx,xx,sysdate);
注明:sysdate 函数用于获取当前系统时间
注明2: 若是往表中所有的列都要添加数据,则可改为:
Insert into table_name
Values (......);
Select * from userinfo;
注明:利用* 表示选择所有的列.
可以在创建表的时候,或者修改表的时候 ,指定默认值: 只需要在对应的列 添加如下语句即可:
Default sysdate
创建新表时候复制表的结构,和数据:
Create table table_name_new
As
Select colunm_name,colunm2_nane..... From table_name;
往表中添加数据时候从其他表中复制:
Insert into table_name(colunm_name,...)
Select colonum_name,....from table_name2;
注意,复制数据时候是按照一列一列的模式添加的,即是要添加的话,则整整一列都会被添加到.
更新数据语句:
Update table_name
Set colunm1=value,......
Where conditions;
当不设置条件时候,整整一列都会被改变,..
若是设置了条件,则只会改变对应的行.
删除数据:
Delete from table_name
Where conditions;
注意:删除数据是按照一行一行的方式来删除的,若是不加上条件,则会把整整一个表的的数据清空.
若是加上条件,则只删除对应的行.
非空约束: not null
把非空约束去掉: null
主键约束: constraint constraint_name
Primary key(.....)
约束改名:
Alter table table_name
Rename constraint constraint_name to new_name;
禁用/启用 约束,但是不删除:
Alter table table_name
Disable/ enable constraint constraint_name;
删除约束:
Alter table table_name
Drop constraint constraint_name;
删除主键:
Alter table table_name
Drop primary key;
约束数据字典: user_constranits
创建表时候可以在列级别,表级别添加外键约束:
修改表时候也可以在列级别,表级别添加外键约束:
References main_table_name(column_name);
Constraint constraint_name
Foreign key (column_name) references main_table_name(column_name) on delete cascade;
注意: on delete cascade 是级联删除的意思,就是说在主表之中的某一列的某一个数据被删除后,,则在从表之中对应的某一行数据都会被删除.
唯一约束:
Unique
Constraint constraint_name
Unique(column_name);
检查约束:
Check (expression)
Constraint constraint_name
Check (expression)
注意:约束名字必须都是唯一的.
基本查询语句:
Select distinct * from table
Where conditions;
注意:distinct 表示显示时候 去掉重复的列,就是说,重复的列只显示一个.
设置显示格式:
① 给字段添加别名,注意这个别名只是显示时候使用,真正的基本表中并没有改变名字.
Column column_name heading new_name;
这种别名的使用可以在显示时候用中文显示.
② 显示时候设置字段的格式;例如控制字符的输出位数,数字的个数,是否添加美元符号等等.
Column column_name format a10/ $999.99;
注意:控制字符个数时候,使用格式为a10.a为表示字符,10表示有10个位置.
若是数字个数时候,一个9表示一个位置,例如99.99
加上个$符号,则显示会多出一个美元符号.
③ 清除掉列的格式:
Column column_name clear;
设置别名的另外一种方式,
在查询时候顺便设置别名,这种方式与上面相比可以一条语句同时设置多个字段.
Select distinct column_name as new_name, column_name as new_name from table_name;
Oracle支持的运算符:
算术运算符:+ - * /
逻辑运算符: and or not
关系运算符: .> = <> <
在查询语句中还可以这样子操作,并且操作结果不影响基本表:
Select id +100 from userinfo where salary>200 and salary <>1000;
运算符的优先级:
比较运算符高于逻辑运算符
逻辑运算符中:NOT>AND>OR
模糊查询:
简单的使用like运算符 和 通配符 _ % 即可
例如: where name like _a%;
_ 可表示一个字符,,
%表示若干个字符.
例子: select * from userinfo where username like ‘_a%’;
范围查询:
只要查询时候指定了范围都可以是范围查询.
例如 where salary >10 and salary <45;
但是,针对范围查询,加多了两组关键字, between and
In / not in;
例如: where salary between 10 and 45;
表示的范围为:闭区间[10,45]
Where salary in (10,45);
In 后面加上集合,用小括号表示,相当于数组.
对查询结果的排序:
只需要在查询语句后面,加上:
Order by column_name desc,column_name2 asc;
Desc表示降序,,,,asc 表示升序.
后面的字段排序,只有在前面字段排序完毕后,相等情况才起作用.
Case ......when...的使用:
格式1: select id, case id when 1 then ‘xx’ when 2 then ‘aaaa’ else ‘qqq’ end as ‘编号’ from userinfo;
格式2: select id, case when id=45,then ‘xx’ when id=1 then ‘www’ else ‘aaa’ end as ‘编号’ from userinfo;
Decode函数的作用效果跟case ...when ...一模一样
格式: decode(column_name,values,result,values,result,defaultvalues);
例子:select id, decode(id,1,’aaa’,2,’wwww’) as ‘编号’ from userinfo;