【www.shanpow.com--经典语句大全】
oracle创建表篇一:oracle基本建表语句
--创建用户create user han identified by han default tablespaceusers Temporary TABLESPACE Temp;grant connect,resource,dba to han; //授予用户han开发人员的权利
--------------------对表的操作----------------------------创建表create table classes( id number(9) not null primary key, classname varchar2(40) not null) --查询表select * from classes;
--删除表drop table students;
--修改表的名称rename alist_table_copy to alist_table;
--显示表结构describe test --不对没查到
-----------------------对字段的操作-------------------------------------增加列alter table test add address varchar2(40);
--删除列alter table test drop column address;
--修改列的名称alter table test modify address addresses varchar(40;
--修改列的属性alter table test modi
create table test1( id number(9) primary key not null, name varchar2(34) )rename test2 to test;
--创建自增的序列create sequence class_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;
select class_seq.currval from dual
--插入数据insert into classes values(class_seq.nextval,"软件一班")commit;
--更新数据update stu_account set username="aaa" where count_id=2;commit;
--创建唯一索引create unique index username on stu_account(username); --唯一索引不能插入相同的数据
--行锁 在新打开的对话中不能对此行进行操作select * from stu_account t where t.count_id=2 for update; --行锁
--alter table stuinfo modify sty_id to stu_id;
alter table students drop constraint class_fk;alter table students add constraint class_fk foreign key (class_id) references classes(id);--外键约束alter table stuinfo add constraint stu_fk foreign key (stu_id) references students(id) ON DELETE CASCADE;--外键约束,级联删除
alter table stuinfo drop constant stu_fk;
insert into students values(stu_seq.nextval,"张三",1,sysdate);
insert into stuinfo values(stu_seq.currval,"威海");
select * from stuinfo;
create table zhuce( zc_id number(9) not null primary key, stu_id number(9) not null, zhucetime date default sysdate
)
create table feiyong ( fy_id number(9) not null primary key, stu_id number(9) not null, mx_id number(9) not null, yijiao number(7,2) not null default 0, qianfei number(7,2) not null )
create talbe fymingxi( mx_id number(9) not null primary key, feiyong number(7,2) not null, //共7位数字,小数后有两位 class_id number(9) not null}
create table card( card_id number(9) primary key, stu_id number(9) not null, money number(7,2) not null default 0, status number(1) not null default 0 --0表可用,1表挂失)
--链表查询
select c.classname||"_"||s.stu_name as 班级_姓名,si.address from classes c,students s , stuinfo si where c.id=s.class_id and s.id=si.stu_id; insert into students values(stu_seq.nextval,"李四",1,sysdate);insert into stuinfo values(stu_seq.currval,"南京");
--函数select rownum,id,stu_name from students t order by id asc;
--中间表实现多对多关联--(1 1, 1 n,n 1,n n )
--1 n的描述 1的表不作处理 n的表有1表的字段--1 1的描述 主外键关联--n n的描述 中间表实现多对多关联
create table course( course_id number(9) not null, couser_name varchar2(40) not null)alter table course to couse;create table stu_couse( stu_couse_id number(9) primary key, stu_id number(9) not null, couse_id number(9) not null
)
create unique index stu_couse_unq on stu_couse(stu_id,couse_id); --唯一学生create sequence stu_couse_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;
create sequence couses_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;insert into course values(couses_seq.nextval,"计算机原理");insert into course values(couses_seq.nextval,"编译原理");insert into course values(couses_seq.nextval,"数据库原理");insert into course values(couses_seq.nextval,"数据结构");insert into course values(couses_seq.nextval,"计算机基础");insert into course values(couses_seq.nextval,"C语言初步");commit;
insert into stu_couse values(stu_couse_seq.nextval,1,1);insert into stu_couse values(stu_couse_seq.nextval,1,3);insert into stu_couse values(stu_couse_seq.nextval,1,5);insert into stu_couse values(stu_couse_seq.nextval,1,5);
insert into stu_couse values(stu_couse_seq.nextval,2,1);commit;select * from stu_couse;select * from course;
--select s.stu_name,sc.couse_id, c.couser_name from students s,course c,stu_couse sc where stu_id=1
--select couse_id from stu_couse where stu_id=1
select cl.classname,s.stu_name,c.couser_name from stu_couse sc, students s,course c,classes cl where s.id=sc.stu_id and sc.couse_id=c.course_id and s.class_id=cl.id and s.id=1;
--班级——姓名select c.classname,s.stu_name from students s,classes c where s.class_id=c.id and s.id=2;
select * from students s where s.id=2--班级——姓名——课程
select cl.classname,s.stu_name,c.couse_name from stu_couse sc,students s,classes cl,couse c where sc.stu_id=s.id and sc.couse_id=c.couse_id and s.id=26;
--sql 语句的写法,现写出关联到的表,然后写出要查找的字段,第三 写出关联条件 ,记住在写关联到的表时先写数据多的表,这样有助于提高sql的效率
select c.couser_name,s.stu_name from stu_couse sc,students s,course c where c.course_id=1 and c.course_id=sc.couse_id and sc.stu_id=s.id;
select s.stu_name from students s,stu_couse sc where s.id=sc.stu_id group by s.id,s.stu_name;
select c.classname,count(sc.couse_id) from stu_couse sc,students s,classes c where s.class_id=c.id and s.id=sc.stu_id group by c.classname;
select s.stu_name, count(sc.couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id group by s.id,s.stu_name having count(sc.stu_couse_id)>3;班级 学生 选课数量select cl.classname,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id and s.class_id=cl.id group by cl.classname;
--班级 学生 选课数量select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id and s.class_id=cl.id group by s.stu_name;
select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc ,students s,classes cl where sc.stu_id=s.id and s.class_id=cl.id group by s.id;
select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where sc.stu_id=s.id and s.class_id=cl.id group by s.stu_name;--班级 学生 所选课程id 所选课程名称
--创建试图 目的把表联合起来 然后看成一个表,在与其他的联合进行查询 create view xsxk as select cl.classname, s.stu_name,c.couse_id, c.couse_name from stu_couse sc,students s,classes cl,couse c where sc.stu_id=s.id and sc.couse_id=c.couse_id and s.class_id=cl.id;
select * from xsxk
create view classstu as select s.id,c.classname,s.stu_name from students s,classes c where c.id=s.class_id;drop view classstu; --删除视图select * from classstu;create view stu_couse_view as select s.id ,c.couse_name from stu_couse sc,students s,couse c where s.id=sc.stu_id and sc.couse_id=c.couse_id;select * from stu_couse_view;create view csc as select cs.classname,cs.stu_name,scv.couse_name from classstu cs,stu_couse_view scv where cs.id=scv.id;select * from csc;
select * from classes cross join students; --全连接,相当于select * from classes,students;
select * from classes cl left join students s on cl.id=s.class_id; --左连接 不管左表有没有 都显示出来select * from classes cl right join students s on cl.id=s.class_id; --右连接select * from classes cl full join students s on cl.id=s.class_id; --全连接
insert into classes values(class_seq.nextval,"软件四班");
create table sales( nian varchar2(4), yeji number(5) );insert into sales values("2001",200);insert into sales values("2002",300);insert into sales values("2003",400);insert into sales values("2004",500);commit;select * from sales;drop table sale;
select s1.nian,sum(s2.yeji) from sales s1,sales s2 where s1.nian>=s2.nian group by s1.nian order by s1.nian desc;
select s1.nian,sum(s2.yeji) from sales s1,sales s2 where s1.nian>=s2.nian group by s1.nian;
s 年 年业绩总和 2001 200 2002 500 2003 900 2004 1400
create table test1( t_id number(4));
create table org( org_id number(9) not null primary key, org_name varchar2(40) not null, parent_id number(9));
create sequence org_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;drop sequence org_seq;insert into org values(1,"华建集团",0);insert into org values(2,"华建集团一分公司",1);insert into org values(3,"华建集团二分公司",1);insert into org values(4,"华建集团财务部",1);insert into org values(5,"华建集团工程部",1);insert into org values(6,"华建集团一分公司财务处",2);insert into org values(7,"华建集团一分公司工程处",2);
select * from org;--不正确 不能实现循环select b.org_id , b.org_name ,b.parent_id from org a,org b where a.org_id=7 and a.parent_id=b.org_id;select * from org connect by prior parent_id=org_id start with org_id=7 order by org_id;select * from org connect by prior org_id=parent_id start with org_id=1 order by org_id;
create table chengji( cj_id number(9) not null primary key, stu_cou_id number(9) not null, fen number(4,1) );insert into chengji values(1,1,62);insert into chengji values(2,2,90);insert into chengji values(3,3,85);insert into chengji values(4,4,45);insert into chengji values(5,5,68);insert into chengji values(6,6,87);commit;select * from chengji;select * from stu_couse;--在oracle 中好像不适用 alter table chengji change stu_cou_id stu_couse_id;alter table shop_jb change price1 price double;
学生姓名 平均分select s.stu_name,avg(cj.fen) from stu_couse sc,chengji cj,students s where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group by s.id,s.stu_name;select s.stu_name from students s,stu_couse sc,chengji cj where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group by s.id,s.stu_name;select s.stu_name,cj.fen from students s,stu_couse sc,chengji cj where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60;
学生姓名 科目 成绩select s.stu_name,c.couse_name,cj.fen from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60 order by=;
select * from stu_couse;
--集合运算--选择了课程3的学生 union 选择了课程5的学生 并集--选择了课程3 或者 选择了课程5的学生select s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=3unionselect s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=5
--选择了课程3,5,2 的学生 intersect 选择课程1,2,4的学生 交集--求选择了课程 2 并且 选择了课程 3 的学生 交集select s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=2intersectselect s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=3;
--选择了课程3,5,8的学生 minus 选择了课程1,7,8的学生 --差集-- 求所有课程的成绩都大于 60 的学生 差集select distinct(s.stu_name) from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60minusselect distinct(s.stu_name) from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen<60;
oracle创建表篇二:一个完整的Oracle建表的例子
一个完整的Oracle建表的例子
建表一般来说是个挺简单的事情,但是Oracle的建表语句有很多可选的参数,有些我们可能平时不太用,
用的时候又不知道怎么用,这里就写一个较完整的建表的例子:
CREATE TABLE banping
(id NUMBER(5)
CONSTRAINT banping_id_pk PRIMARY KEY,
last_name VARCHAR2(10)
CONSTRAINT banping_last_name_nn NOT NULL,
first_name VARCHAR2(10) NOT NULL UNIQUE,
userid VARCHAR2(8)
CONSTRAINT banping_userid_uk UNIQUE,
start_date DATE DEFAULT SYSDATE,
title VARCHAR2(10),
dept_id NUMBER(7)
CONSTRAINT banping_dept_id_fk REFERENCES dept(id),
salary NUMBER(11,2),
user_type VARCHAR2(4)
CONSTRAINT banping_user_type_ck CHECK
(user_type IN("IN","OUT")),
CONSTRAINT banping_uk_title UNIQUE (title,salary)
)
INITRANS 1 MAXTRANS 255
PCTFREE 20 PCTUSED 50
STORAGE( INITIAL 1024K NEXT 1024K PCTINCREASE 0 MINEXTENTS 1
MAXEXTENTS 5)
TABLESPACE data
;
解释说明如下:
语法
CREATE TABLE [schema.]table
(column datatype [, column datatype] … )
[TABLESPACE tablespace]
[PCTFREE integer]
[PCTUSED integer]
[INITRANS integer]
[MAXTRANS integer]
[STORAGE storage-clause]
[LOGGING | NOLOGGING]
[CACHE | NOCACHE] ];
Schema:表的所有者
Table:表名
Column:字段名
Datatype:字段的数据类型
Tablespace:表所在的表空间
Pctfree:为了行长度增长而在每个块中保留的空间的量(以占整个空间减去块头部后所剩余空间的百分比形式表示),当剩余空间不足pctfree时,
不再向该块中增加新行。
Pctused:在块剩余空间不足pctfree后,块已使用空间百分比必须小于pctused后,才能向该块中增加新行。
INITRANS:在块中预先分配的事务项数,缺省值为1
MAXTRANS:限定可以分配给每个块的最大事务项数,缺省值为255
STORAGE:标识决定如何将区分配给表的存储子句
LOGGING:指定表的创建将记录到重做日志文件中。它还指定所有针对该表的后续操作都将被记录下来。这是缺省设置。
NOLOGGING:指定表的创建将不被记录到重做日志文件中。
CACHE:指定即使在执行全表扫描时,为该表检索的块也将放置在缓冲区高速缓存的LRU列表最近使用的一端。
NOCACHE:指定在执行全表扫描时,为该表检索的块将放置在缓冲区高速缓存的LRU列表最近未使用的一端。
STORAGE子句:
INITIAL:初始区的大小
NEXT:下一个区的大小
PCTINCREASE:以后每个区空间增长的百分比
MINEXTENTS:段中初始区的数量
MAXEXTENTS:最大能扩展的区数
如果已为表空间定义了MINIMUM EXTENT,则表的区大小将向上舍入为MINIMUM EXTENT值的下一个较高的倍数。
外键关联的表dept的id列必须是唯一的或者是自身的主键,如不是可以用以下语句填加:
alter table dept add constraint dept_id_pk primary
key(id)
块空间使用参数可用来控制对数据段和索引段空间的使用:
控制并发性参数:
INITRANS和MAXTRANS指定初始的和最大的事务位置数,这些事务位置在索引块或者数据块内创建。事务位置用来存储在某一事件点上正在对
块进行更改的事务的信息。一个事务只占用一个事务位置,即使它正在更改多行或者多个索引条目。
INITRANS对数据段的缺省值为1,对索引段的缺省值为2,以保证最低程度的并发。例如,如果INITRANS设为3,则保证至少3个事务可以同时对
块进行更改。如果需要,也可以从块空闲空间内分配其它事务位置,以允许更多的事务并发修改块内的行。
MAXTRANS的缺省值为255,它设置可更改数据块或者索引块的并发事务数的限制。设置后,该值限制事务位置对空间的使用,从而保证块内有足够的空间
供行或者索引数据使用。
控制数据空间使用的参数:
数据段的PCTFREE指定每个数据块中保留空间的百分比,用于因更新块内的行而导致的增长。PCTFREE的缺省值为10%。
数据段的PCTUSED代表Oracle服务器试图为表内的每个数据块维持的已用空间的最低百分比。如果一个块的已用空间低于PCTUSED,则将这块放
回到空闲列表中。段的空闲列表示容纳将来所插入内容的可选择块的列表。根据缺省,每个段在创建时都有一个空闲列表。PCTUSED的缺省值为40%。
PCTFREE和PCTUSED都按可用数据空间百分比来计算,可用数据空间是从整个块大小减去块头空间后剩余的块空间。
块空间使用参数只能针对段指定,而不能在表空间级别设置。
下面步骤介绍对PCTFREE=20且PCTUSED=40的数据段如何管理块内空间:
1.向块中插入行,直到块内的空闲空间小等于20%。当行所占用的块内数据空间达到80%(100-PCTFREE)或者更多后,即无法再向该块进
行插入。
2.剩余的20%可在行大小增长时使用。例如,更新初始为NULL的列并分配一个值。这样,由于更新,块使用率可能超过80%。
3.如果由于更新,删除了块内的行或者行大小减少,块使用率可能跌至80%以下。但是,仍然无法向块中插入,直到块使用率跌至PCTUSED以下,
在本例中PCTUSED为40%。
4.当块使用率跌至PCTUSED以下后,该块可用于插入。随着向块内插入行,块使用率增长,重复从步骤1开始的循环。
oracle创建表篇三:oracle 创建表空间详细介绍
注意点: 1.如果在PL/SQL 等工具里打开的话,直接修改下面的代码中[斜体加粗部分]执行 2.确保路径存在,比如【D:\oracle\oradata\Oracle9i\】也就是你要保存文件的路径存在 /*分为四步 */ /*第1步:创建临时表空间 */
复制代码 代码如下:
create temporary tablespace user_temp tempfile "D:\oracle\oradata\Oracle9i\user_temp.dbf" size 50m autoextend on next 50m maxsize 20480m extent management local;
/*第2步:创建数据表空间 */
复制代码 代码如下:
create tablespace user_data logging datafile "D:\oracle\oradata\Oracle9i\user_data.dbf" size 50m autoextend on next 50m maxsize 20480m extent management local;
/*第3步:创建用户并指定表空间 */
复制代码 代码如下:
create user username identified by password default tablespace user_data temporary tablespace user_temp;
/*第4步:给用户授予权限 */
复制代码 代码如下:
grant connect,resource,dba to username;