************************table 表************************
------------------------字段类型------------------------
varchar2 变长字串最长4096个字节,多长占多少,效率低
char 固定长度 char 8 存 'abc'也占8个,效率高
number8,3 8位小数点3位
date 年月日时分秒
long最长2g
blob clob
建立学生表,学号、姓名
create table stu
(
id number(6),
name varchar2(20),
sex number(1),
age number(3),
sdate date,
grade number(2) default 1,
class number(4),
email varchar2(50)
);
----------------------------约束条件---------------------
字段级约束条,加在字段后,不能完成组合唯一,比如name和email组合不能重复(这种用表级约束),
表级约束在最后表的最后一个字段后加入,例如 constaint stu_name_emil_nui unique(email,name)
约束条件也是个对象可以给他起名字 关键字constraint 如 name varchar2(20) constraint stu_name_nn not null。
五个约束条件
1.非空 not nll,
2.唯一 unique 不能往这个字段插入重复值(字段级)如email varchar2(50) unique,
表级约束在最后表的最后一个字段后加入,例如 constraint stu_name_emil _nui unique(email,name),
3.主键 primary key可以唯一标示整条记录的东西 非空 唯一,例如字段级id number(6) primary key,
也可以表级,constraint stu_name_emil_pk primary (id),
4.外键 references 可以加在本表的2个字段,也可以加在2个表的字段上,被参考的字段必须是主键
例如 class number(4) references class(id),
表级constraint stu_class_fk foreign key class (id) references class(id)
5.check。
create table stu
(
id number(6),
name varchar2(20) constraint stu_name_nn not null,
sex number(1),
age number(3),
sdate date,
grade number(2) default 1,
class number(4) ,
email varchar2(50) ,
constraint stu_class_fk foreign key (class) references class(id),
constraint stu_id_pk primary key(id),
constraint stu_name_emil_nui unique(email,name)
);
create table class
(
id number(4) ,
------------------------ALTER TABLE更改表结构-----------------------
增加字段
alter table stu add(addr varchar2(100));
修改
alter table stu modify(addr varchar2(50));
删除字段
alter table stu drop(addr);
去掉约束条件
alter table stu drop constraint stu_class_fk;
修改约束条件
先删掉约束,再加进来。
alter table stu add constraint stu_class_fk foreign key (class) references class(id);
*************************Oracle的数据字典表***********************
oracle 中把当前用户有哪些表,视图, 约束放到一个表中。一般称其为数据字典表。
比如user_tables,user_views;user_constraints
select table_name from user_tables;
------------------user_tables---------------
TABLE_NAME
DEPT
EMP
BONUS
SALGRADE
EMP2
DEPT2
SALGRADE2
CLASS
STU
数据字典表的信息储存在数据字典表的字典表dictionary;
desc dictionary
名称 是否为空? 类型
TABLE_NAME VARCHAR2(30)
COMMENTS VARCHAR2(4000)
*************************Oracle的索引index***********************
索引的用处,是访问数据时读效率高,修改反而慢了。
create index idx_stu_email on stu(email);
drop index idx_stu_email;
******************************视图view******************************
视图就是个子查询,以v$开头,用来简化查询,保护数据,但增加了维护代价。视图可以用来更新数据,但很少用到,因为可能同时更新很多表
create view v$_stu as select id,name,age from stu;
*****************************序列 sequence**************************
Oracle特有的用于产生唯一的不间断一组数字序列。
create table artile
(
id number,
title varchar2(1024),
cont long
);
create sequence seq;
insert into artile values(sql.nextval,'a','b');