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

Oracle学习笔记四 --- 变量及表管理

2016-04-03 18:41 567 查看

1、表名、列命名规则
字母开头
不能超过30字符
不能使用oracle关键字
有些特别字符不能用

2、变量
--字符型
char 定长 最大2000字符
例子:char(10) '小韩’前四个字符放'小韩’,后6个字符放空格:“小韩 ”
查询效率极快,经常要查询的字段可以用这个类型

varchar2 变长 最大4000字符
例子:char(10) '小韩’oracle仅分配四个字符放'小韩’,这样可以节约空间

clob (character larg object) 字符型大对象 最大4G 对应text

--数字型
number 范围 -10^38 ~ 10^38 整数、小数都可以
number(5,2) 表示一个小数有5位有效数,2位小数 -999.99 ~ 99999
number(5) 表示一个五位整数 -99999 ~ 99999

--日期类型
date 包含年月日 时分秒,插入时格式看下面
timestamp oracle9i对date的扩展,精度更高一点

--其他
blob 二进制数据 4G 图片、声音等 一般只有安全性要求高的才放这里啦

3、建表
create table student (
xh number(4),
xm varchar2(20),
sex char(2),
birthday date,
sal number(7,2)
);

表空间 Oracle表空间概念.note

添加一个字段
alter table [表名] add [字段名] [字段类型]
alter table student add classid number(2);
修改字段长度
alter table [表名] modify [字段名] [字段类型]
alter table student modify xm varchar2(30);
删除一个字段 (仅对空表操作才安全)
alter table [表名] drop [字段名]

alter table student drop column sal;
修改表名字
rename [表名] to [新表名]
rename student to stu;
删除表
drop table [表名]
drop table student;

添加数据
insert into student values('A001','张三','男','01-5月-05')

日期格式
这里日期默认顺序需要这样:'DD-MON-YY' dd日子(天) mon(月份) yy 2位的年 ’09-6月-99‘ 1999.6.9

修改日期格式
alter session set nls_date_format='yyyy-mm-dd'; -- 这样就修改格式为“1999-6-9”

插入空值
insert into student(xh,xm,set,birthday) values(3,'aa','男',null);

查找

根据NULL值查找时,不能用: =null 或 ='' 等

SQL> insert into student values(1002,'小明','男', null);
已创建 1 行。
SQL> select * from student;
XH XM SEX BIRTHDAY
---------- ---------------------------------------- ------ --------------
1001 小明 男 09-5月 -92
1002 小明 男
SQL> select * from student where birthday = '';
未选定行
SQL> select * from student where birthday is null;
XH XM SEX BIRTHDAY
---------- ---------------------------------------- ------ --------------
1002 小明 男

SQL> select * from student where birthday is not null;
XH XM SEX BIRTHDAY
---------- ---------------------------------------- ------ --------------
1001 小明 男 09-5月 -92

SQL> select * from student where birthday = '09-5月-92';
XH XM SEX BIRTHDAY
---------- ---------------------------------------- ------ --------------
1001 小明 男 09-5月 -92

修改字段

update student set xm='小张', sex='女' where xh=1002;

修改多个字段用逗号隔开

创建保存点和回滚
save point a;
rollback;

删除数据
delete from student; 删除所有记录,表结构还在,写日志,可以恢复,速度慢
drop table sudent; 删除表结构和数据
truncate table student; 删除所有记录,表结构还在,不写日志,可以恢复,速度快
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: