Oracle-基本建表语句
oracle数据库基本语句
oracle数据库基本语句oracle 数据库是一种常用的关系型数据库管理系统,常用的oracle数据库包括oracle10g、oracle11g和oracle12c。
要掌握oracle的基本用法,必须掌握其基本的语句。
oracle的常用基本语句有:一、数据定义语言(DDL)1、创建数据表:CREATE TABLE 表名(字段名数据类型[完整性约束条件],字段名数据类型[完整性约束条件],……);2、修改数据表: ALTER TABLE 表名 ADD(字段名数据类型[完整性约束条件],字段名数据类型[完整性约束条件],……);3、删除数据表: DROP TABLE 表名;4、创建索引:CREATE [UNIQUE] INDEX 索引名ON 表名[字段名[,字段名];5、删除索引: DROP INDEX 索引名;三、数据控制语言(DCL)1、建立用户: CREATE USER 用户名 IDENTIFIED BY 密码;2、删除用户: DROP USER 用户名;3、授权:GRANT 权限 ON 对象 TO 用户[WITH GRANT OPTION];4、回收授权: REVOKR 权限 ON 对象 FROM 用户;5、控制事务: COMMIT/ROLLBACK;四、数据库控制语言(DBCL)1、创建数据库:CREATE DATABASE 数据库名;2、删除数据库: DROP DATABASE 数据库名;3、创建表空间:CREATE TABLESPACE 表空间名 SEGMENT SPACE MANAGEMENT 自动;4、删除表空间: DROP TABLESPACE 表空间名;5、管理会话: ALTER SYSTEM KILL SESSION ['会话号'];。
【Oracle】需要快速建表时常用到的语句
【Oracle】需要快速建表时常⽤到的语句-- 查emp表是否存在SQL>select count(*) from user_tables where table_name=upper('emp');COUNT(*)----------1-- 删除表SQL>drop table emp;表已删除。
-- 创建表SQL>create table emp(2 id int,3 name nvarchar2(20),4primary key(id));表已创建。
-- 给表插值SQL>insert into emp2select rownum,dbms_random.string('*',dbms_random.value(6,20))3from dual4 connect by level<3001;已创建3000⾏。
-- 给表增加⼀列SQL>alter table emp add(age int);表已更改。
-- 更新表中字段SQL>update emp set age=dbms_random.value(18,65) where1=1;已更新3000⾏。
-- 设置name⼀列的宽度,为查询做准备SQL>column name format a30;-- 查询有限条⽬SQL>select*from emp where rownum<6;ID NAME AGE---------- ------------------------------ ----------1 OQTXGOYACMTC 422 ZUMTOSZUQDLRGXKULA 413 GTTIQENYUVLUXIDL 274 SRLGYXFWRHBZ 535 TIFNKNDHKNJEURYHXDY 55-- 增加⼀列SQL>alter table emp add(dept int);表已更改。
create table oracle语句
create table oracle语句
嘿,朋友们!今天咱就来好好唠唠这个“create table oracle 语句”。
就好像盖房子得先有个设计图一样,这语句就是在数据库里搭建“房子”的关键呀!
比如说,你想在数据库里建个表来存你的宝贝数据,那这“create table”语句就派上大用场啦!就像你打算给自己建个秘密基地,得先规
划好怎么建呀。
“create table 表名 (列名数据类型, 列名数据类型……)”,看,这就
是它的基本模样。
这就好比你给基地划分不同的区域,这个区域放玩具,那个区域放零食。
咱来举个具体例子哈。
比如说你要建个学生表,里面有学生的学号、姓名、年龄这些信息。
那你就可以这样写:create table students (id number, name varchar2(50), age number)。
哇塞,是不是感觉很神奇?就
这么简单的几行语句,一个能存学生信息的“小房子”就建起来啦!
这“create table oracle 语句”多重要啊,没有它,你的数据就像没家
的孩子一样到处流浪呢!它就像一个超级建筑师,能按照你的要求把
表建得稳稳当当的。
再想想,要是没有它,那数据不就乱套啦?那可不行呀!它可不就
是我们管理数据的得力小助手嘛!所以呀,咱可得好好掌握这个语句,让它为我们服务呀!
我的观点就是,“create table oracle 语句”真的是数据库世界里超级重要的存在呀,我们必须得好好学它、用它!。
oracle create hr table建表语句
oracle create hr table建表语句
在Oracle数据库中,创建一个名为"hr"的表需要使用CREATE TABLE语句。
以下是一个示例建表语句,用于创建一个简单的"hr"表:
sql复制代码
CREATE TABLE hr (
id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
email VARCHAR2(100),
hire_date DATE
);
这个示例创建了一个名为"hr"的表,具有以下列:
•id:一个整数类型的列,作为主键。
•first_name:一个最大长度为50的字符串列,用于存储员工的名字。
•last_name:一个最大长度为50的字符串列,用于存储员工的姓氏。
•email:一个最大长度为100的字符串列,用于存储员工的电子邮件地址。
•hire_date:一个日期类型的列,用于存储员工的入职日期。
请注意,这只是一个示例建表语句,你可以根据自己的需求进行修改和扩展。
在实际应用中,你可能需要添加更多的列和约束条件,以满足特定的业务需求。
Oracle数据库建表及其基本操作
1.在对oracle数据操作之前,需要了解oracle的类型,oracle的常用类型有:2.登录创建好的用户,然后在表空间中建立表,以userinfo表为例:--创建表create table userinfo(id number(6,0),usernam varchar2(20),userpwd varchar2(20),email varchar2(30),regdate date);表建立成功之后,查看是否建表成功,有很多方法,列举我常用的两个方法:方法一:在控制台输入desc userinfo 回车, 可以成功查询出新建表字段的类型方法二:使用图形化工具直接查询表select * from userinfo;3.添加字段alter table table_nameadd column_name datatype;table_name是表名称, column_name是列名称, datatype是数据类型eg:添加一个字段remark字段,是varchar2类型,长度为100alter table userinfoadd remarks varchar2(100);查看表结构:给字段添加注释comment on column 表.列 is '列注释';eg:comment on column new_uesrinfo.remarks is '注释';在plsql上面的运行结果:4.修改表字段名称和表名--给字段改名alter table table_namerename column column_name To new_column_name; --修改表的名字rename table_name to new_table_name;5.删除字段alter table table_namedrop column column_name;6.更改字段长度,类型eg:修改remarks字段修改字段的长度alter table userinfomodify remarks varchar2(150);eg:修改字段类型a.如果表中没有数据alter table 表名 modify(字段名1 类型,字段名2 类型,字段名3 类型.....)b.如果表中有数据(分为四步)--修改原字段名ALTER TABLE 表名 RENAME COLUMN 字段名A TO 字段名B;--添加一个和原字段同名的字段"字段名A",添加的字段ALTER TABLE 表名 ADD 字段名目标类型;--将"字段名B"的数据更新到新建字段"字段名A"中UPDATE 表名 SET 字段名A = TIRM(字段名B);--更新完成后,删除原备份字段"字段名B"ALTER TABLE 表名 DROP COLUMN 字段名Beg:例如将上表的remarks字段修改为number(32,2)alter table userinfo rename column remarks to remarks_tmp;alter table userinfo add remarks number(32,2);update userinfo set remarks = trim(remarks_tmp);alter table userinfo drop column remarks_tmp;7.删除数据,删除表--删除表数据truncate table table_name;delete from table_name这里truncate和delete的区别是,truncate是截断了表,它的效率比delete要快--删除表的结构drop table table_name;。
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 modicreate 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=1select 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 wheres.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,studentss,classes c where s.class_id=c.id and s.id=sc.stu_id group byc.classname;select s.stu_name, count(sc.couse_id) from stu_couse sc,studentss,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 ands.class_id=cl.id group bycl.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 xsxkcreate 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 wherecs.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 wheres1.nian>=s2.nian group by s1.nian order by s1.nian desc;select s1.nian,sum(s2.yeji) from sales s1,sales s2 wheres1.nian>=s2.nian group by s1.nian;s年年业绩总和2001 2002002 5002003 9002004 1400create 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 /doc/5e8716366.html_id , /doc/5e8716366.html_name ,b.parent_id from org a,org b where/doc/5e8716366.html_id=7 and a.parent_id=/doc/5e8716366.html_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_idstu_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 andsc.stu_couse_id=cj.stu_couse_id group bys.id,s.stu_name;select s.stu_name from students s,stu_couse sc,chengji cj wheres.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group bys.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,studentss,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 wheres.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 wheres.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 wheres.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 wheres.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 andsc.couse_id=c.couse_id andsc.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 andsc.couse_id=c.couse_id andsc.stu_couse_id=cj.stu_couse_id and cj.fen<60;⼀、何謂分區表(索引)?分區表是⼀種數據庫的物理存儲機制,它將⼀個表的數據存儲在不同的存儲⽚段中,這些不同的存儲⽚稱為區(partition)。
Oracle数据库语句大全
Oracle数据库语句大全一.入门部分1.创建表空间create tablespace schooltbs datafile ‘D:\oracle\datasource\schooltbs.dbf’ size 10M autoextend on;2.删除表空间drop tablespace schooltbs[including contents and datafiles];3.查询表空间基本信息select *||tablespace_name from DBA_TABLESPACES;4.创建用户create user lihuaidentified by lihuadefault tablespace schooltbstemporary tablespace temp;5.更改用户alter user lihuaidentified by 123default tablespace users;6.锁定用户alter user lihua account lock|unlock;7.删除用户drop user lihua cascade;--删除用户模式8.oracle数据库中的角色connect,dba,select_catalog_role,delete_catalog_role,execute_catalo g_role,exp_full_database,imp_full_database,resource9.授予连接服务器的角色grant connect to lihua;10.授予使用表空间的角色grant resource to lihua with grant option;--该用户也有授权的权限11.授予操作表的权限grant select,insert on user_tbl to scott;--当前用户grant delete,update on er_tbl to scott;--系统管理员二.SQL查询和SQL函数1.SQl支持的命令:数据定义语言(DDL):create,alter,drop数据操纵语言(DML):insert,delete,update,select数据控制语言(DCL):grant,revoke事务控制语言(TCL):commit,savepoint,rollback2.Oracle数据类型字符,数值,日期,RAW,LOB字符型char:1-2000字节的定长字符varchar2:1-4000字节的变长字符long:2GB的变长字符注意:一个表中最多可有一列为long型Long列不能定义唯一约束或主键约束long列上不能创建索引过程或存储过程不能接受long类型的参数。
Oracle创建表语句(Createtable)语法详解及示例
Oracle创建表语句(Createtable)语法详解及⽰例创建表(Create table)语法详解1. ORACLE常⽤的字段类型ORACLE常⽤的字段类型有VARCHAR2 (size) 可变长度的字符串, 必须规定长度CHAR(size) 固定长度的字符串, 不规定长度默认值为1NUMBER(p,s) 数字型p是位数总长度, s是⼩数的长度, 可存负数最长38位. 不够位时会四舍五⼊.DATE ⽇期和时间类型LOB 超长字符, 最⼤可达4GCLOB 超长⽂本字符串BLOB 超长⼆进制字符串BFILE 超长⼆进制字符串, 保存在外的⽂件⾥是只读的.数字字段类型位数及其四舍五⼊的结果原始数值1234567.89数字字段类型位数存储的值Number 1234567.89Number(8) 12345678Number(6) 错Number(9,1) 1234567.9Number(9,3) 错Number(7,2) 错Number(5,-2) 1234600Number(5,-4) 1230000Number(*,1) 1234567.92. 创建表时给字段加默认值和约束条件创建表时可以给字段加上默认值例如 : ⽇期字段 DEFAULT SYSDATE这样每次插⼊和修改时, 不⽤程序操作这个字段都能得到动作的时间创建表时可以给字段加上约束条件例如: ⾮空 NOT NULL不允许重复 UNIQUE关键字 PRIMARY KEY按条件检查 CHECK (条件)外键 REFERENCES 表名(字段名)3. 创建表的例⼦CREATE TABLE DEPT(EPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,DNAME VARCHAR2(14),LOC VARCHAR2(13)) ;CREATE TABLE region(ID number(2) NOT NULL PRIMARY KEY,postcode number(6) default '0' NOT NULL,areaname varchar2(30) default ' ' NOT NULL);4. 创建表时的命名规则和注意事项1)表名和字段名的命名规则:必须以字母开头,可以含符号A-Z,a-z,0-9,_,$,#2)⼤⼩写不区分3)不⽤SQL⾥的保留字, ⼀定要⽤时可⽤双引号把字符串括起来.4)⽤和实体或属性相关的英⽂符号长度有⼀定的限制注意事项:1)建表时可以⽤中⽂的字段名, 但最好还是⽤英⽂的字段名2)创建表时要把较⼩的不为空的字段放在前⾯, 可能为空的字段放在后⾯3)建表时如果有唯⼀关键字或者唯⼀的约束条件,建表时⾃动建了索引4)⼀个表的最多字段个数也是有限制的,254个.5. 约束名的命名规则和语法约束名的命名规则约束名如果在建表的时候没有指明,系统命名规则是SYS_Cn(n是数字)约束名字符串的命名规则同于表和字段名的命名规则6. 使⽤约束时的注意事项约束⾥不能⽤系统函数,如SYSDATE和别的表的字段⽐较可以⽤本表内字段的⽐较想在事务处理后, 做约束的检查SQL> alter session set constraints deferred.7. 由实体关系图到创建表的例⼦ s_dept前提条件:已有region表且含唯⼀关键字的字段idSQL> CREATE TABLE s_dept(id NUMBER(7)CONSTRAINT s_dept_id_pk PRIMARY KEY,name VARCHAR2(25)CONSTRAINT s_dept_name_nn NOT NULL,region_id NUMBER(7)CONSTRAINT s_dept_region_id_fk REFERENCES region (id),CONSTRAINT s_dept_name_region_id_uk UNIQUE(name, region_id));8. 较复杂的创建表例⼦SQL> CREATE TABLE s_emp(id NUMBER(7)CONSTRAINT s_emp_id_pk PRIMARY KEY,last_name VARCHAR2(25)CONSTRAINT s_emp_last_name_nn NOT NULL,first_name VARCHAR2(25),userid VARCHAR2(8)CONSTRAINT s_emp_userid_nn NOT NULLCONSTRAINT s_emp_userid_uk UNIQUE,start_date DATE DEFAULT SYSDATE,comments VARCHAR2(25),manager_id NUMBER(7),title VARCHAR2(25),dept_id NUMBER(7)CONSTRAINT s_emp_dept_id_fk REFERENCES s_dept(id),salary NUMBER(11,2),commission_pct NUMBER(4,2)CONSTRAINT s_emp_commission_pct_ck CHECK(commission_pct IN(10,12.5,15,17.5,20)));8. 通过⼦查询建表通过⼦查询建表的例⼦SQL>CREATE TABLE emp_41 AS SELECT id, last_name, userid, start_dateFROM s_emp WHERE dept_id = 41;SQL> CREATE TABLE A as select * from B where 1=2;只要表的结构.10. ⽤⼦查询建表的注意事项1)可以关连多个表及⽤集合函数⽣成新表,注意选择出来的字段必须有合法的字段名称,且不能重复。
date数据类型 oracle建表语句
date数据类型 oracle建表语句在Oracle数据库中,我们可以使用DATE数据类型来存储日期和时间的信息。
DATE数据类型表示的是从公元前4712年1月1日的午夜开始到指定日期的天数。
在创建表时,我们可以使用以下的语法来定义一个包含DATE数据类型的列:CREATE TABLE 表名(列名 DATE);下面是一个具体的示例来说明如何使用DATE数据类型创建一个包含日期信息的表:CREATE TABLE employees(emp_id NUMBER,emp_name VARCHAR2(100),hire_date DATE);在上面的示例中,我们创建了一个名为employees的表,其中包含了emp_id、emp_name和hire_date这三个列。
其中hire_date列的数据类型为DATE,表示雇佣日期的信息。
当我们插入数据时,可以使用TO_DATE函数将字符串转换为DATE类型,然后再将其插入到表中。
以下是一个插入数据的示例:INSERT INTO employees (emp_id, emp_name, hire_date)VALUES (1, 'John Doe', TO_DATE('2022-08-01', 'YYYY-MM-DD'));在上面的示例中,我们使用了TO_DATE函数将字符串'2022-08-01'转换为DATE类型,并将其插入到了表中的hire_date列中。
当我们查询数据时,可以使用TO_CHAR函数将DATE类型的数据转换为字符串,以便更好地显示和处理。
以下是一个查询数据的示例:SELECT emp_id, emp_name, TO_CHAR(hire_date, 'YYYY-MM-DD') AShire_dateFROM employees;在上面的示例中,我们使用了TO_CHAR函数将日期数据hire_date转换为字符串,并将其作为列别名显示在查询结果中。
magic-api oracle建表语句
magic-api oracle建表语句
在 Oracle 数据库中,可以使用 `CREATE TABLE` 语句来创建表。
以下是一个简单的示例,展示了如何使用 Oracle 建表语句来创建一个名为 `employees` 的表:
```sql
CREATE TABLE employees (
employee_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
hire_date DATE,
salary NUMBER(10, 2)
);
```
上面的建表语句创建了一个 `employees` 表,包含了以下列:
- `employee_id`:员工 ID,是一个 6 位数的整数,作为主键(PRIMARY KEY)。
- `first_name`:员工的名字,使用 `VARCHAR2(50)` 类型,最大长度为 50。
- `last_name`:员工的姓氏,使用 `VARCHAR2(50)` 类型,最大长度为 50。
- `hire_date`:员工的入职日期,使用 `DATE` 类型。
- `salary`:员工的薪水,使用 `NUMBER(10, 2)` 类型,表示最大 10 位数且有两位小数的数字。
请注意,以上建表语句只是一个示例,你可以根据你的需求进行修改和调整。
oracle标准建表语句
oracle标准建表语句
Oracle标准建表语句的基本语法如下:
sql复制代码
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
其中,table_name是要创建的表的名称,column1, column2, column3等是表的列名,datatype是列的数据类型。
在建表时,你可以为每列指定一个数据类型,例如:
•NUMBER:用于存储整数和浮点数。
•VARCHAR2:用于存储可变长度的字符串。
•DATE:用于存储日期和时间。
•CLOB:用于存储大型文本数据。
•等等。
你还可以为表中的某些列设置约束,例如:
•NOT NULL:指定该列不能为空。
•PRIMARY KEY:指定该列作为主键,用于唯一标识表中的每一行。
•FOREIGN KEY:指定该列是外键,用于建立表之间的关系。
•UNIQUE:指定该列的唯一性约束,确保该列中的值都是唯一的。
•等等。
以下是一个具体的例子,创建一个名为employees的表,包含id, name, 和salary三个列:
sql复制代码
CREATE TABLE employees (
id NUMBER PRIMARY KEY,
name VARCHAR2(50),
salary NUMBER(10, 2)
);
这只是一个简单的例子,实际上Oracle 支持更多的语法和功能,可以根据具体需求进行调整。
oracle系统表建表语句
oracle系统表建表语句1. 你想知道在Oracle里怎么建系统表吗?就像盖房子得有蓝图一样,建表也得有正确的语句。
比如说,要创建一个简单的员工信息表,CREATE TABLE employee (id NUMBER, name VARCHAR2(50), age NUMBER); 这里就定义了表名是employee,有id、name和age这几个列呢。
2. 哇塞,Oracle系统表建表语句可没那么神秘!想象你在做一个收纳盒,每个格子放不同的东西。
CREATE TABLE product (product_id NUMBER, product_name VARCHAR2(100), price NUMBER); 这就像做了个放产品信息的收纳盒,产品编号、名称和价格各有各的“格子”。
3. Oracle建表语句就像是给数据安排宿舍。
我给你举个例子,要是创建一个学生成绩表,CREATE TABLE score (student_id NUMBER, subject VARCHAR2(50),grade NUMBER); 就像给每个学生的科目成绩都安排了一个小“宿舍”呢。
4. 嘿建Oracle系统表的语句不难理解啦。
好比你要整理自己的书架,每个架子放不同类型的书。
CREATE TABLE book (book_id NUMBER, book_title VARCHAR2(100), author VARCHAR2(50)); 这就是给书的编号、书名和作者建了个“书架”。
5. 哎你可别觉得Oracle建表语句很复杂哦。
你看,如果要建一个订单表,就像给订单们造个家。
CREATE TABLE order_table (order_id NUMBER, customer_name VARCHAR2(100), order_date DATE); 这样订单的编号、顾客名字和日期都有地方住啦。
6. 天呐,建Oracle系统表就像在画画。
oracle11g写建表语句
下是一个示例,用800字回答了如何创建一个简单的用户信息表:首先,我们需要创建一个名为"users"的表,用于存储用户的基本信息。
以下是一个示例建表语句:```sqlCREATE TABLE users (id NUMBER(10) PRIMARY KEY,username VARCHAR2(50) NOT NULL,password VARCHAR2(50) NOT NULL,email VARCHAR2(100),created_at DATE DEFAULT SYSDATE,updated_at DATE);```解释如下:* `CREATE TABLE`语句用于创建一个新表。
* 表名为"users",表示存储用户信息的表。
* `id`字段是主键,使用`NUMBER(10)`数据类型,表示一个数字字段,最多可以存储10位数字。
* `username`和`password`字段分别表示用户名和密码,使用`VARCHAR2(50)`数据类型,表示可变长度的字符字段,最多可以存储50个字符。
这些字段的值不能为空(NOT NULL),因为它们用于存储用户的身份验证信息。
* `email`字段用于存储用户的电子邮件地址,使用`VARCHAR2(100)`数据类型,最多可以存储100个字符。
* `created_at`和`updated_at`字段分别表示用户的创建时间和更新时间,使用`DATE`数据类型。
这两个字段的值默认为当前日期(SYSDATE),用于记录用户的活动历史。
此外,为了提高查询效率,我们还可以为表添加索引来加速查询。
以下是一个示例创建索引的语句:```sqlCREATE INDEX idx_users_username ON users (username);```这个索引将加速对用户名字段的查询操作。
除了上述示例中的基本结构,您还可以根据实际需求添加其他字段和约束,例如用户角色、手机号码、地址等。
oracle查询表的建表语句
oracle查询表的建表语句表1: 学生信息表CREATE TABLE student_info (student_id NUMBER(10) PRIMARY KEY, -- 学生IDstudent_name VARCHAR2(20) NOT NULL, -- 学生姓名gender CHAR(1) CHECK (gender IN ('M', 'F')), -- 性别age NUMBER(2), -- 年龄address VARCHAR2(100), -- 地址phone_number VARCHAR2(20) UNIQUE, -- 手机号码email VARCHAR2(50), -- 邮箱birth_date DATE -- 出生日期);表2: 课程信息表CREATE TABLE course_info (course_id NUMBER(5) PRIMARY KEY, -- 课程IDcourse_name VARCHAR2(50) NOT NULL, -- 课程名称credit NUMBER(1), -- 学分department VARCHAR2(50), -- 开设院系teacher_id NUMBER(10), -- 教师IDCONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher_info(teacher_id) -- 外键关联教师信息表);表3: 教师信息表CREATE TABLE teacher_info (teacher_id NUMBER(10) PRIMARY KEY, -- 教师IDteacher_name VARCHAR2(20) NOT NULL, -- 教师姓名gender CHAR(1) CHECK (gender IN ('M', 'F')), -- 性别age NUMBER(2), -- 年龄address VARCHAR2(100), -- 地址phone_number VARCHAR2(20) UNIQUE, -- 手机号码email VARCHAR2(50) -- 邮箱);表4: 成绩表CREATE TABLE score_info (student_id NUMBER(10), -- 学生IDcourse_id NUMBER(5), -- 课程IDscore NUMBER(3), -- 成绩CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES student_info(student_id), -- 外键关联学生信息表CONSTRAINT fk_course_id FOREIGN KEY (course_id)REFERENCES course_info(course_id) -- 外键关联课程信息表);表5: 班级信息表CREATE TABLE class_info (class_id NUMBER(5) PRIMARY KEY, -- 班级IDclass_name VARCHAR2(20) NOT NULL, -- 班级名称department VARCHAR2(50) NOT NULL, -- 所属院系teacher_id NUMBER(10), -- 班主任教师IDCONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher_info(teacher_id) -- 外键关联教师信息表);表6: 学生选课表CREATE TABLE student_course (student_id NUMBER(10), -- 学生IDcourse_id NUMBER(5), -- 课程IDCONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES student_info(student_id), -- 外键关联学生信息表CONSTRAINT fk_course_id FOREIGN KEY (course_id) REFERENCES course_info(course_id) -- 外键关联课程信息表);表7: 学生考勤表CREATE TABLE student_attendance (student_id NUMBER(10), -- 学生IDcourse_id NUMBER(5), -- 课程IDattendance_date DATE, -- 考勤日期status CHAR(1) CHECK (status IN ('P', 'A')), -- 考勤状态 (P: 出勤, A: 缺勤)CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES student_info(student_id), -- 外键关联学生信息表CONSTRAINT fk_course_id FOREIGN KEY (course_id) REFERENCES course_info(course_id) -- 外键关联课程信息表);表8: 课程安排表CREATE TABLE course_schedule (course_id NUMBER(5), -- 课程IDweekday VARCHAR2(20), -- 星期几start_time VARCHAR2(10), -- 开始时间end_time VARCHAR2(10), -- 结束时间classroom VARCHAR2(20), -- 上课教室CONSTRAINT fk_course_id FOREIGN KEY (course_id) REFERENCES course_info(course_id) -- 外键关联课程信息表);表9: 教师授课表CREATE TABLE teacher_course (teacher_id NUMBER(10), -- 教师IDcourse_id NUMBER(5), -- 课程IDCONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher_info(teacher_id), -- 外键关联教师信息表CONSTRAINT fk_course_id FOREIGN KEY (course_id) REFERENCES course_info(course_id) -- 外键关联课程信息表);表10: 学生班级关系表CREATE TABLE student_class (student_id NUMBER(10), -- 学生IDclass_id NUMBER(5), -- 班级IDCONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES student_info(student_id), -- 外键关联学生信息表CONSTRAINT fk_class_id FOREIGN KEY (class_id) REFERENCES class_info(class_id) -- 外键关联班级信息表。
Oracle建表空间各种语句
Oracle建表空间各种语句第一篇:Oracle建表空间各种语句在创建用户之前,先要创建表空间:其格式为:格式: create tablespace 表间名 datafile '数据文件名' size 表空间大小;如:SQL> create tablespace news_tablespace datafile 'F:oracleproduct10.1.0oradatanewsnews_data.dbf' size 500M;其中'news_tablespace'是你自定义的表空间名称,可以任意取名;'F:oracleproduct10.1.0oradatanewsnews_data.dbf'是数据文件的存放位置,'news_data.dbf'文件名也是任意取;'size 500M'是指定该数据文件的大小,也就是表空间的大小。
现在建好了名为'news_tablespace'的表空间,下面就可以创建用户了:其格式为:格式: create user 用户名 identified by 密码 default tablespace 表空间表;如:SQL> create user news identified by news default tablespace news_tablespace;默认表空间'default tablespace'使用上面创建的表空间。
接着授权给新建的用户:SQL> grant connect,resource to news;--表示把connect,resource权限授予news用户SQL> grant dba to news;--表示把 dba权限授予给news用户授权成功。
ok!数据库用户创建完成,现在你就可以使用该用户创建数据表了!1.建表空间create tablespace hoteldata datafile 'D:javaOracleproduct10.1.0oradatazznorclhoteldata.dbf'size200m autoextend on next 10m maxsize unlimited;2.建用户create user hotel identified by hotel default tablespace hoteldataaccount unlock;//identified by 后面的是密码,前面的是用户名3.用户授权grant resource,connect,RECOVERY_CATALOG_OWNER to hotel;grant create table to hotel;alter user hotel quota unlimited ON OSDB;alter user hotel default tablespace hoteldata;4.删除表空间DROP TABLESPACE hoteldata INCLUDING CONTENTS AND DATAFILES;5.删除用户DROP USER hotel CASCADE 6.删除表的注意事项在删除一个表中的全部数据时,须使用TRUNCATE TABLE 表名;因为用DROP TABLE,DELETE * FROM 表名时,TABLESPACE表空间该表的占用空间并未释放,反复几次DROP,DELETE操作后,该TABLESPACE上百兆的空间就被耗光了。
oracle语句大全及用法
oracle语句大全及用法Oracle语句是Oracle数据库中用于管理和操作数据的SQL语句。
以下是一些常用的Oracle语句及其用法:1. 数据定义语句(DDL)CREATE DATABASE:用于创建新的数据库。
CREATE TABLE:用于创建新的表。
语法如下:sql`CREATE TABLE table_name (column1 datatype1, column2 datatype2, ...);`ALTER TABLE:用于修改现有的表结构,如添加、删除或修改列。
DROP TABLE:用于删除现有的表。
CREATE INDEX:用于在表上创建索引,以提高查询性能。
2. 数据操纵语句(DML)SELECT:用于从表中查询数据。
语法如下:sql`SELECT column1, column2, ... FROM table_name;`INSERT INTO:用于向表中插入新的数据行。
语法如下:sql`INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);`UPDATE:用于修改表中的现有数据。
DELETE:用于从表中删除数据。
3. 数据控制语句(DCL)GRANT:用于授予用户或角色对数据库对象的访问权限。
REVOKE:用于撤销用户或角色对数据库对象的访问权限。
4. 事务控制语句(TCL)COMMIT:用于提交当前事务,使所做的更改永久生效。
ROLLBACK:用于撤销当前事务,恢复到事务开始前的状态。
SAVEPOINT:用于在事务中设置保存点,以便在之后的某个时刻可以回滚到该点。
5. 其他常用语句DECLARE:用于声明变量或常量,并为其分配数据类型和初始值。
BEGIN ... END:用于定义PL/SQL代码块的开始和结束。
IF ... THEN ... ELSE:用于条件判断,根据条件执行不同的操作。
oracle数据库建表语句
educational practice, is to strengthen the state-owned enterprise party construction, keep the party's advanced nature and purity, consolidating the party's ruling foundation and ruling status of inevitable requirement. The party's advanced nature and the party's ruling status is not a once and for all, immutable. Past advanced does not equal the present advanced, now advanced is not equal to advanced forever. In the past have not equal to now have, now have is not equal to always have. Keep the party's advanced nature and purity, consolidating the party's ruling foundation and ruling position, is the party's Facing the construction of the fundamental problem and the subject of the times. Central enterprises is completed a comprehensive well-off society in an important force, is an important pillar of socialism with Chinese characteristics, is an important basis for the Communist Party of China. To carry out the party's mass line of educational practice, is to further strengthen and improve the party's leadership, play the exemplaryvanguard role of Party members, the people honest and pragmatic value pursuit deeply rooted in the thoughts and actions of all Party members, and keep the party's advanced nature and purity. It is to hold to a party to want to manage the party, strictly, so as to promote the ideological and working style construction of the party building in all respects, Party of the body and the party ranks of self purification, self end At the beginning of the good, self innovation, self - improvement; is to further development of Zhuang country economy and enhance control of state-owned enterprises, influence and vitality to further consolidate the party's ruling foundation, to consolidate the party's ruling status. Third, to carry out the party's mass line of educational practice, is vigorously carry forward the spirit of "Three Gorges", "four winds" in the control group of scientific development outstanding issues to solve the inevitable requirement. The central government decided to the mass line of educational practice is mainly focus on stylebuilding, efforts to solve formalism, bureaucratism, hedonism and extravagant wind "four winds". In the process of long-term construction of the Three Gorges project, the company Always maintain a hard work, truth-seeking and pragmatic, scientific and democratic, solidarity and cooperation of the fine style of work, unity and lead all the Three Gorges builders tenacious struggle, the successful completion of the Three Gorges project construction tasks, to the party and the people on the a qualified answer. Second, conscientiously implement the central spirit, solidly carry out the party's mass line of educational practice central the education practice of guiding ideology, objectives, tasks, basic principles, methods, steps made specific provisions, Party accordingly formulated the < on the in-depth development of the party's mass line of educational practice implementation scheme. Party organizations at all levels must conscientiously implement the spirit of the central government, do a good job of scheme To carry out theexecution, guarantee the educational practice to achieve the worker masses satisfactory effect. Is the guiding ideology of the Corporation of Party's mass line of educational practice, hold high the great banner of socialism with Chinese characteristics, and comprehensively implement the party's eighteen big spirits, to Marxism Leninism, Mao Zedong thought, Deng Xiaoping Theory and the important thought of "Three Represents" and the scientific development concept as a guide, closely around the keep the party's advanced nature and purity that pragmatic and honest people as the main content, the implementation of the provisions of the central eight as a starting point, strengthen the education of the Marxist view of the masses and the party's mass line, with excellent style ningxinjuli for The construction of the Three Gorges project, the development of the Yangtze River, building a world-class large-scale clean energy group to provide a strong guarantee. The theme of group company of educational practice is changing thestyle, ningxinjuli, for the construction of a world-class clean energy group to provide a strong guarantee. Methods: take the lead of the party, from top to bottom. The focus of education is the leading bodies at all levels, leading organs and deputy division level and above cadres. Focusing the outstanding problem is: oppose formalism, bureaucratism, hedonism and extravagant wind. By resolutely opposed "to the four winds", efforts to solve outstanding problems of Party members and cadres in the party spirit party party, affecting the outstanding problems of scientific development to the group company , the outstanding problems of the workers and the masses are strongly. To achieve the goal of requirements are: enhance the ideological quality of the cadres as well as to effectively change the style of work, close party masses relationship, establish image of pragmatic and honest people of. According to the requirements of the central, group company as Wuguan enterprises to participate in the party's mass line of the first batchof educational practice. Among them, group leadership and the departments of the headquarters of educational practice, by the central leadership and sent a steering group. Group owned enterprises and units in late August started, under the leadership of the Party group carry out educational practice, by the party sent a steering group of supervision and inspection. Corporation group and subordinate units of Education Practice time staggered, stubble, in accordance with the requirements of the quality of the progress of the subject of for complete educational practice at the end of November. In accordance with the provisions of the central, education and practice activities divided into three links to. The first link is learning education, listen to their views. The focus of this part is to make a good job in the publicity and ideological education, carry out investigation and study, listen to the opinions of the masses of workers, for revealing and criticism to prepare. Consider to propaganda, focus on learning and education, held a forum,to seek the views of the tasks are relatively heavy, the first link time arranged more than a month, to the end of August. The key learning education is to improve the Ideological awareness, firmly establish the Marxist view of the masses, bearing in mind the wholeheartedly the fundamental purpose of serving the people. To me to learn the spirit of the central document, carefully read the < on the mass line - an important discussion excerpts >, selected documents > < the party's mass line of educational practice in learning, < austerity against waste - an important discussion excerpts > the learning materials, learning Xi Jinping and other leading comrades of the Central Committee on strengthening the construction of style of a series of important exposition, learning the glorious history of the CPC and the fine tradition of ideological reality, the transformation of the subjective world, building prison ideological line of defense. Explicitly requested by the party, the party members and cadres to participate in learning when Roomshall be not less than 30 hours, on business to timely make up a missed lesson, deputy division level and above cadres to writing reading experience. In the learning stage of education, party to schedule three discussion focused on learning. Is around the stick to the mass line of the party, masses viewpoint, how close party group do group carry out study and discussion topics; the second is based on anti-corruption and maintain cadres honest invite experts to give lectures, to further enhance the ability to resist corruption and cadres; three is to implement the provisions of the central eight carry out inspections, to change the style, ningxinjuli, for the construction of a world-class clean energy group provide strong guarantee as the theme to carry out special topic for discussion.5555555555555555555555555555555555555555555555555555555555555oracle数据库建表语句--创建用户 create user han identified by han default tablespace users 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 --不对没查到刚才,XX汇报了一季度经济运行情况;其它30多个市直单位、个县市区印发了书面材料,大家对形势的分析都比较理性、客观,对做好下阶段的工作信心较足、措施具体;几位市领导结合分管工作,谈了很好的意见;等会,易书记还将作重要讲话,大家务必认真领会精神,切实抓好落实。
Oracle建表空间各种语句
在创建用户之前,先要创建表空间:其格式为:格式: create tablespace 表间名 datafile '数据文件名' size 表空间大小;如:SQL> create tablespace news_tablespace datafile'F:\oracle\product\10.1.0\oradata\news\news_data.dbf' size 500M;其中'news_tablespace'是你自定义的表空间名称,可以任意取名;'F:\oracle\product\10.1.0\oradata\news\news_data.dbf'是数据文件的存放位置,'news_data.dbf'文件名也是任意取;'size 500M'是指定该数据文件的大小,也就是表空间的大小。
现在建好了名为'news_tablespace'的表空间,下面就可以创建用户了:其格式为:格式: create user 用户名 identified by 密码 default tablespace 表空间表;如:SQL> create user news identified by news default tablespace news_tablespace;默认表空间'default tablespace'使用上面创建的表空间。
接着授权给新建的用户:SQL> grant connect,resource to news; --表示把 connect,resource权限授予news用户SQL> grant dba to news; --表示把 dba权限授予给news用户授权成功。
ok! 数据库用户创建完成,现在你就可以使用该用户创建数据表了!1.建表空间create tablespace hoteldata datafile'D:\java\Oracle\product\10.1.0\oradata\zznorcl\hoteldata.dbf'size200m autoextend on next 10m maxsize unlimited;2.建用户create user hotel identified by hotel default tablespace hoteldata account unlock;//identified by 后面的是密码,前面的是用户名3.用户授权grant resource,connect,RECOVERY_CATALOG_OWNER to hotel ;grant create table to hotel;alter user hotel quota unlimited ON OSDB;alter user hotel default tablespace hoteldata;4.删除表空间DROP TABLESPACE hoteldata INCLUDING CONTENTS AND DATAFILES;5.删除用户DROP USER hotel CASCADE6.删除表的注意事项在删除一个表中的全部数据时,须使用TRUNCATE TABLE 表名;因为用DROP TABLE,DELETE * FROM 表名时,TABLESPACE表空间该表的占用空间并未释放,反复几次DROP,DELETE操作后,该TABLESPACE上百兆的空间就被耗光了。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
--创建用户create user han identified by han default tablespaceusers Temporary TABLESPACE Temp;grant connect,resource,dba to han; //授予用户han开发人员的权利--------------------对表的操作--------------------------创建表格语法:create table 表名(字段名1 字段类型(长度) 是否为空,字段名2 字段类型是否为空);-增加主键alter table 表名 add constraint 主键名 primary key (字段名1);-增加外键:alter table 表名add constraint 外键名 foreign key (字段名1)references 关联表 (字段名2);在建立表格时就指定主键和外键create table T_STU (STU_ID char(5)not null,STU_NAME varchar2(8)not null,constraint PK_T_STU primary key (STU_ID));主键和外键一起建立:create table T_SCORE (EXAM_SCORE number(5,2),EXAM_DATE date,AUTOID number(10)not null,STU_ID char(5),SUB_ID char(3),constraint PK_T_SCORE primary key (AUTOID),constraint FK_T_SCORE_REFE foreign key (STU_ID)references T_STU (STU_ID))--创建表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 modicreate 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 ands.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=1select cl.classname,s.stu_name,c.couser_name from stu_couse sc, students s,course c,classes cl where s.id=sc.stu_id andsc.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 wheres.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,studentss,classes c where s.class_id=c.id and s.id=sc.stu_id group byc.classname;select s.stu_name, count(sc.couse_id) from stu_couse sc,studentss,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 bycl.classname;--班级学生选课数量select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_cousesc,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 xsxkcreate 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 wheres1.nian>=s2.nian group by s1.nian order by s1.nian desc;select s1.nian,sum(s2.yeji) from sales s1,sales s2 wheres1.nian>=s2.nian group by s1.nian;s年年业绩总和2001 2002002 5002003 9002004 1400create 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 _id , _name ,b.parent_id from org a,org b where_id=7 and a.parent_id=_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_idstu_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 bys.id,s.stu_name;select s.stu_name from students s,stu_couse sc,chengji cj wheres.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group bys.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,studentss,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 wheres.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 wheres.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 wheres.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 wheres.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 andsc.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 andsc.stu_couse_id=cj.stu_couse_id and cj.fen<60;。