当前位置:文档之家› Oracle-基本建表语句资料讲解

Oracle-基本建表语句资料讲解

Oracle-基本建表语句资料讲解
Oracle-基本建表语句资料讲解

--创建用户

create user han identified by han default tablespace

users 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 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 https://www.doczj.com/doc/3616781667.html,_id , https://www.doczj.com/doc/3616781667.html,_name ,b.parent_id from org a,org b where

https://www.doczj.com/doc/3616781667.html,_id=7 and a.parent_id=

https://www.doczj.com/doc/3616781667.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_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=3

union

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=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=2

intersect

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=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>60

minus

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<60;

ORACLE常用SQL语句大全

ORACLE常用SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not nul l],..) 根据已有的表创建新表: A:select * into table_new from table_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle> 5、说明:删除表 drop table tablename

6、说明:增加一个列,删除一个列 A:alter table tabname add column col type B:alter table tabname drop column colname 注:DB2DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、添加主键: Alter table tabname add primary key(col) 删除主键: Alter table tabname drop primary key(col) 8、创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、创建视图:create view viewname as select statement 删除视图:drop view viewname 10、几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、几个高级查询运算词 A:UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 B:EXCEPT 运算符 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 C:INTERSECT 运算符 INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。 12、使用外连接

Oracle-基本建表语句

--创建用户 create user han identified by han default tablespace users 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 modi

Oracle查询语句基本命令一

oracle查询语句大全--基本命令大全一 1.create user username identified by password;//建用户名和密码oracle ,oracle 2.grant connect,resource,dba to username;//授权grant connect,resource,dba,sysdba to username; 3.connect username/password//进入。 4.select table_name,column_name from user_tab_columns where table_name='mview_log';//查询表中的表名,字段名等等。 5. 如何执行脚本SQL文件? SQL>@PATH/filename.sql; 6.Oracle oledb 提供者在command中执行多条SQL语句与SQL SERVER有少许差别,SQL Server只需使用";"分割多条SQL语句,而Oracle需要遵守ORACLE调用规范,即除分号分割外,还需以begin /end;包围语句体. 使用C#描述应如下所示: https://www.doczj.com/doc/3616781667.html,mandText = "begin INSERT INTO GROUP_INFO (GROUP_ID, GROUP_NAME) V ALUES (1, \'2\'); INSERT INTO GROUP_INFO(GROUP_ID, GROUP_NAME) V ALUES (2, \'2\'); end;"; 7.查询用户下的表的信息select distinct table_name from user_tab_columns; 8.如何搜索出前N条记录?Select a.*,rownum from (select * from cardkind order by cardkind ) a where rownum show user 3、查看系统拥有哪些用户SQL> select * from all_users; 4、新建用户并授权 SQL> create user a identified by a;(默认建在SYSTEM表空间下) SQL> grant connect,resource to a; 5、连接到新用户SQL> conn a/a

ORACLE基本SQL语句

ORACLE基本定义、操作语句 一、表 1.创建表 CREATE TABLE TAB_NAME ( COL_01 V ARCHAR2(10) NOT NULL, COL_02 NUMBER(8,2), COL_03 DATE ); 2.添加主键约束 ALTER TABLE TAB_NAME ADD CONSTRAINT PK_COL_01 PRIMARY KEY(COL_01); 3.添加唯一性约束 ALTER TABLE TAB_NAME ADD CONSTRAINT UK_COL_02 UNIQUE(COL_02); 4.添加外键约束 ALTER TABLE TAB_NAME ADD CONSTRAINT FK_COL0_03 FOREIGN KEY(COL_03) REFERENCES TAB_2(COL_03); 5.添加check约束 ALTER TABLE TAB_NAME ADD CONSTRAINT CHK_COL_03 CHECK(COL_01 <> ‘ABC’); 6.创建索引 ――创建唯一索引 CREATE UNIQUE INDEX IDX_NAME ON TAB_NAME(COL_01); ――创建非唯一索引 CREATE INDEX IDX_NAME ON TAB_NAME(COL_01); 7.给表添加一个新列 ALTER TABLE TAB_NAME ADD COL_04 V ARCHAR2(10); 8.修改列的数据类型 ALTER TABLE TAB_NAME MODIFY COL_04 NUMBER(8); 9.删除一列 ALTER TABLE TAB_NAME DROP COLUMN COL_04; 10.更改表的名称 ALTER TABLE TAB_NAME RENAME TO TAB_NEW_NAME; 11.更改表的列名 ALTER TABLE TAB_NAME RENAME COLUMN COL_04 TO COL_05; 12.给表和列添加注释说明 --add comments to the table COMMENT ON TABLE TAB_NAME IS‘示例表’; --add comments to the column COMMENT ON COLUMN TAB_NAME.COL_01 IS‘列名’; 13.删除表 DROP TABLE TAB_NAME; 14.MERGE merge into tj_test1 tt1

oracle9i常用操作

ORACLE9I日常操作 获取数据库日期 select sysdate from dual; 一、ORACLE的启动和关闭 1、在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a、启动ORACLE系统 oracle>svrmgrl SVRMGR>connect internal SVRMGR>startup SVRMGR>quit b、关闭ORACLE系统 oracle>svrmgrl SVRMGR>connect internal SVRMGR>shutdown SVRMGR>quit 启动oracle9i数据库命令: $ sqlplus /nolog SQL*Plus: Release 9.2.0.1.0 - Production on Fri Oct 31 13:53:53 2003 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. SQL> connect / as sysdba Connected to an idle instance. SQL> startup^C SQL> startup ORACLE instance started. 2、在双机环境下 要想启动或关闭ORACLE系统必须首先切换到root用户,如下 su -root a、启动ORACLE系统 hareg -y oracle b、关闭ORACLE系统 hareg -n oracle Oracle数据库有哪几种启动方式 说明: 有以下几种启动方式: 1、startup nomount 非安装启动,这种方式启动下可执行:重建控制文件、重建数据库 读取init.ora文件,启动instance,即启动SGA和后台进程,这种启动只需要init.ora文件。

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 lihua identified by lihua default tablespace schooltbs temporary tablespace temp; 5.更改用户 alter user lihua identified by 123 default 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,resource 9.授予连接服务器的角色 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 https://www.doczj.com/doc/3616781667.html,er_tbl to scott;--系统管理员 二.SQL查询和SQL函数 1.SQl支持的命令: 数据定义语言(DDL):create,alter,drop 数据操纵语言(DML):insert,delete,update,select 数据控制语言(DCL):grant,revoke 事务控制语言(TCL):commit,savepoint,rollback 2.Oracle数据类型 字符,数值,日期,RAW,LOB 字符型 char:1-2000字节的定长字符

Oracle数据库常用的Sql语句

Oracle数据库常用的Sql语句 今天想查询一下Oracle数据库下所有的表名或某个用户下的所有表,半天没想起来.还是在网上找到了答案. select table_name from all_tables;//所有的表明 select table_name from user_all_tables;//用户的所有的表 一下是转贴的sql语句的帖子. select * from user_objects; //查询所有的表 select * from dba_tables; //查询所有的表 select * from all_tables; //查询所有的表 select * from user_users //查出一个用户 select * from all_users //查询所有用户 select * from dba_users //查询所有用户 select name,dbid from v$database; //查询数据库名和它的ID select * from https://www.doczj.com/doc/3616781667.html,er_tab_columns; //查询表名,并显示列名 describe 表名//查询表结构 2: 查询数据库参数 show parameter db; 3:查询数据库的实例名 select instance_name from v$instance; 4: 数据库域名 数据库安装结束后,如果要知道正在运行额数据库是否有域名以及数据库域名名称可以用select value from v$parameter where name='db_domain' show parameter domain 5:数据库服务名 如果数据库有域名,则数据库服务名就是全局数据库名,如果该数据库没有定义域名,则数据库服务名与数据库名相同 show parameter service_name 6:显示当前用户 show user 7:直接登陆 sqlplus "/as sysdba" 8:当前ORACLE系统时间 select sysdate from dual; 9:查询数据库字典v$nls_parameter产看字符集相关参数 select * from v$nls_parameters; //************* oracle基本操作语句(适合初学者) oracle操作语句:

oracle基本操作语句(适合初学者)

1. select * from table_name where rownum>begin and rownum< end 2.sql = "select * from table" con.prepareCall("SELECT * FROM(SELECT A.*, rownum r FROM("+sql+") A WHERE rownum <= "+intPage*intPageSize+") B WHERE r > "+(intPage-1) *intPageSize); 今天想查询一下Oracle数据库下所有的表名或某个用户下的所有表,半天没想起来.还是在网上找到了答案. select table_name from all_tables;//所有的表明 select table_name from user_all_tables;//用户的所有的表 一下是转贴的sql语句的帖子. select * from user_objects; //查询所有的表 select * from dba_tables; //查询所有的表 select * from all_tables; //查询所有的表 select * from user_users //查出一个用户 select * from all_users //查询所有用户 select * from dba_users //查询所有用户 select name,dbid from v$database; //查询数据库名和它的ID select * from https://www.doczj.com/doc/3616781667.html,er_tab_columns; //查询表名,并显示列名 describe 表名//查询表结构 select * from https://www.doczj.com/doc/3616781667.html,er_tab_columns where table_name=表名//查询指定表名的字段 2: 查询数据库参数 show parameter db;

oracle数据库常用语句

数据类型:字符类型:char(标准通用拉丁字符),nchar(汉字等其他字符),varchar2(长度可变字符),nvarchar2,long; 数字类型:number(通用),integer,float 日期和时间:date,timestamps(分秒,时区) 行:rowid(逻辑地址),urowid(逻辑地址,内存地址); 二进制:raw(size)(原始二进制数据),long raw,blob(二进制大型对象;最大4G字节),clob(字符大型对象),nclob,bfile; 2.oracle WEB管理页面:localhost:5560/isqlplus; localhost:5500/em https://www.doczj.com/doc/3616781667.html,设置远程测试:tnsping datebasename;远程连接:sqlplus name/password@datebasename; 4.创建表空间:create tablespace test datafile 'test.dbf' size 10m autoextend on next 2m maxsize unlimited logging permanent extent management local autoallocate blocksize 8k segment space management manuaL;//段空间

5.创建用户并连接: create user "TEST" identified by "TEST" default tablespace TEST temporary tablespace TEMP quota unlimited on TEST quota unlimited on TEMP grant "connect" to test//分配基本权限。 conn test/test; 6.重设用户密码:scott/tiger为默认用户,alter user scott identified by tiger; 解锁:alter user scott account unlock; 7.sql脚本的执行:@路径/filename.sql; 8.创建表:create table t1(c1 type 约束,c2 type 约束(not null,unique,check,primary key)) 9.查询:select distinct c1 from t1 where 条件group by c1 having by 子条件order by c1; 10.连接字符串:select c1 (as可省) 列1 ||c2 from t1;

oracle常用SQL语句(汇总版)

Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象 一.数据控制语句(DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’ 如果字段值里包含单引号’ 需要进行字符串转换, 我们把它替换成两个单引号''. 字符串类型的字段值超过定义的长度会出错, 最好在插入前进行长度校验. 日期字段的字段值可以用当前数据库的系统时间SYSDATE, 精确到秒 或者用字符串转换成日期型函数TO_DATE(‘2001-08-01’,’YYYY-MM-DD’) TO_DATE()还有很多种日期格式, 可以参看ORACLE DOC. 年-月-日小时:分钟:秒的格式YYYY-MM-DD HH24:MI:SS INSERT时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用CLOB类型, 方法借用ORACLE里自带的DBMS_LOB程序包. INSERT时如果要用到从1开始自动增长的序列号, 应该先建立一个序列号 CREATE SEQUENCE 序列号的名称(最好是表名+序列号标记) INCREMENT BY 1 START WITH 1 MAXVALUE 99999 CYCLE NOCACHE;

其中最大的值按字段的长度来定, 如果定义的自动增长的序列号NUMBER(6) , 最大值为999999 INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL 2.DELETE (删除数据表里记录的语句) DELETE FROM表名WHERE 条件; 注意:删除记录并不能释放ORACLE里被占用的数据块表空间. 它只把那些被删除的数据块标成unused. 如果确实要删除一个大表里的全部记录, 可以用TRUNCATE 命令, 它可以释放占用的数据块表空间 TRUNCATE TABLE 表名; 此操作不可回退. 3.UPDATE (修改数据表里记录的语句) UPDATE表名SET 字段名1=值1, 字段名2=值2, …… WHERE 条件; 如果修改的值N没有赋值或定义时, 将把原来的记录内容清为NULL, 最好在修改前进行非空校验; 值N超过定义的长度会出错, 最好在插入前进行长度校验.. 注意事项: A. 以上SQL语句对表都加上了行级锁, 确认完成后, 必须加上事物处理结束的命令COMMIT 才能正式生效, 否则改变不一定写入数据库里. 如果想撤回这些操作, 可以用命令ROLLBACK 复原.

完整版oracle基本操作语句适合初学者

1. select * from table_name where rownum>begin and rownum< end 2.sql = "select * from table" con.prepareCall("SELECT * FROM(SELECT A.*, rownum r FROM("+sql+") A WHERE rownum <= "+intPage*intPageSize+") B WHERE r > "+(intPage-1) *intPageSize); 今天想查询一下 Oracle 数据库下所有的表名或某个用户下的所有表 ,半天没想起来 .还是 在网上找到了答案 . select table_name from all_tables;// 所有的表明 select table_name from user_all_tables;// 用户的所有的表 select * from https://www.doczj.com/doc/3616781667.html,er_tab_columns; // 查询表名 ,并显示列名 describe 表名 //查询表结构 select * from https://www.doczj.com/doc/3616781667.html,er_tab_columns where table_name= 表名 // 查询指定表名的字段 2: 查询数据库参数 show parameter db; 3:查询数据库的实例名 一下是转贴的 sql 语句的帖子 . select * from user_objects; select * from dba_tables; select * from all_tables; select * from user_users select * from all_users select * from dba_users select name,dbid from v$database; //查询所有的表 //查询所有的表 //查询所有的表 //查出一个用户 //查询所有用户 //查询所有用户 // 查询数据库名和它的 ID

oracle中常用的一些语句----"增删改查

如何查找、删除表中重复的记录 方法原理: 1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的, rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。 2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中 那些具有最大rowid的就可以了,其余全部删除。 实现方法: SQL》 create table a ( 2 bm char(4), --编码 3 mc varchar2(20) --名称 4 ) 5 / 表已建立。 SQL》 insert into a values(‘1111’,‘1111’); SQL》 insert into a values(‘1112’,‘1111’); SQL》 insert into a values(‘1113’,‘1111’); SQL》 insert into a values(‘1114’,‘1111’); SQL》 insert into a select * from a; 插入4个记录。 SQL》 commit;

完全提交。 SQL》 select rowid,bm,mc from a; ROWID BM MC ------------------ ---- ------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111 000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111 查询到8记录。 查出重复记录 SQL》 select rowid,bm,mc from a where a.rowid!=(select max(rowid)from a b where a.bm=b.bm and a.mc=b.mc); ROWID BM MC ------------------ ---- -------------------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111

Oracle数据库常用操作语句

Oracle数据库常用操作语句 1.创建目录 system create directory egoms_file as 'd:\dump'; select * from dba_directories; 2.创建表空间 创建临时表空间 create temporary tablespace egoms_temp tempfile 'f:\oracle\data\wpms2\egoms_temp.dbf' size 1024m autoextend on next 32m extent management local; 创建数据表空间 create tablespace egoms_data logging datafile 'f:\oracle\data\wpms2\egoms_data.dbf' size 512m autoextend on next 32m extent management local; 3.创建用户并修改表空间 create user egoms identified by egoms default tablespace egoms_data temporary tablespace egoms_temp; conn system/system@orcl; drop user egoms cascade; select * dba_data_files; 4.赋权限 grant resource,connect,dba,create table to egoms; grant read,write on directory egoms_file to egoms; 5.导出 expdp wpms/ucstcl@orcl directory=egoms_file

oracle基本语句格式

数据定义语言 ?数据定义语言用于改变数据库结构,包括创建、更改和删除数据库对象?用于操纵表结构的数据定义语言命令有: ?CREATE TABLE ?ALTER TABLE ?TRUNCATE TABLE ?DROP TABLE 数据操纵语言 ?数据操纵语言用于检索、插入和修改数据 ?数据操纵语言是最常见的SQL命令 ?数据操纵语言命令包括: ?SELECT ?INSERT ?UPDA TE ?DELETE DML –SELECT 命令 ?利用现有的表创建表 ?语法: CREATE TABLE AS SELECT column_names FROM ; SQL> CREATE TABLE newstudent AS SELECT * FROM student; SQL> CREATE TABLE newstudent1 AS SELECT sno, sname FROM student; SQL> CREATE TABLE newstudent2 AS SELECT * FROM student WHERE 1 = 2; ?选择无重复的行 ?在SELECT子句,使用DISTINCT关键字 SQL> SELECT DISTINCT sname FROM student; ?使用列别名 ?为列表达式提供不同的名称 ?该别名指定了列标题 SQL> SELECT sno 学号, 2010-extract(year from birthday) “年龄”----中间有空格,用“” FROM student; DML –INSERT 命令 ?插入日期类型的值 ?日期数据类型的默认格式为“DD-MON-RR” ?使用日期的默认格式 ?使用TO_DATE函数转换

常用的SQL语句语法(Oracle)ddl和dml分列

一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字段名2, ……) SELECT 字段名1, 字段名 2, …… FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’ 如果字段值里包含单引号’ 需要进行字符串转换, 我们把它替换成两个单引号''. 字符串类型的字段值超过定义的长度会出错, 最好在插入前进行长度校验. 日期字段的字段值可以用当前数据库的系统时间SYSDATE, 精确到秒 或者用字符串转换成日期型函数TO_DATE(‘2001-08-01’,’YYYY-MM-DD’) TO_DATE()还有很多种日期格式, 可以参看ORACLE DOC. 年-月-日小时:分钟:秒的格式YYYY-MM-DD HH24:MI:SS INSERT时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用CLOB类型,方法借用ORACLE里自带的DBMS_LOB程序包. INSERT时如果要用到从1开始自动增长的序列号, 应该先建立一个序列号CREATE SEQUENCE 序列号的名称 (最好是表名+序列号标记) INCREMENT BY 1 START WITH 1 MAXVALUE 99999 CYCLE NOCACHE; 其中最大的值按字段的长度来定, 如果定义的自动增长的序列号 NUMBER(6) , 最大值为999999 INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL 2.DELETE (删除数据表里记录的语句) DELETE FROM表名 WHERE 条件; 注意:删除记录并不能释放ORACLE里被占用的数据块表空间. 它只把那些被删除的数据块标成unused. 如果确实要删除一个大表里的全部记录, 可以用 TRUNCATE 命令, 它可以释放占用的数据块表空间 TRUNCATE TABLE 表名; 此操作不可回退. 3.UPDATE (修改数据表里记录的语句) UPDATE表名 SET 字段名1=值1, 字段名2=值2, …… WHERE 条件; 如果修改的值N(右操作数)没有赋值或定义时, 将把原来的记录内容清为NULL, 最好在修改前进行非空校验; 值N(右操作数)超过定义的长度会出错, 最好在插入前进行长度校验.. 注意事项: A. 以上SQL语句对表都加上了行级锁,

oracle数据库基本操作

实战经验Oracle数据库基本操作步步详解来源:https://www.doczj.com/doc/3616781667.html, 2007年06月12日 15:34网友评论:0条点击: 274 一,约束操作 1:更改约束名称: ALTER TABLE TName RENAME CONSTRAINT oldname TO newname; 2:删除约束 ALTER TABLE TName DROP CONSTRAINTame 3:停止约束 ALTER TABLE TName MODIFY CONSTRAINTame DISABLE; 4:起用约束 ALTER TABLE TName MODIFY CONSTRAINTame ENABLE VALIDATE; 5:新增约束 ALTER TABLE TName ADD CONSTRAINTame FOREIGN KEY (ORG_ID) REFERENCES ref_table (ORGID); 二,字段操作 1:更改字段数据类型: ALTER TABLE TName MODIFY(ORG_ID VARCHAR2(50 BYTE)); 2:更改字段数据长度: ALTER TABLE TName MODIFY(ORG_ID VARCHAR2(80 BYTE)); 3:修改表的列名 alter table TName rename column xx to yy; 三,表操作 1:删除表数据: truncate table TName; 2:复制空表结构 create table new_table as select * from old_table where 1=2; 3:复制表(含记录) create table new_table as select * from old_table ; 四,存储过程 1:命令行编译存储过程 ALTER PROCEDURE procedure_name COMPILE;

ORACLE DBA常用语句

连接命令 conn[ect] 用法: conn 用户名/密码@网络服务名[as sysdba/sysoper] 当用特权用户身份链接时,必须带上as sysdba或者as sysoper 注:sysdba权限最大 disc[onnect] 断开连接 passw[ord] 修改用户的密码 需先登陆 文件操作命令 start或者@ 运行sql脚本 案例@ d:\a.sql spool 将sql*plus屏幕上的内容输出到制定文件中 案例:spool d:\b.sql 并输入spool off 显示和设置环境变量 linesize 设置显示行宽度,默认80 set linesize pagesize 设置每页显示的行数目,默认14 用户管理 权限: 系统权限:用户对数据库的相关权限 对象权限:用户对其他用户的数据对象(该用户创建的数据:表视图过程)访问权限(select,insert updata delete all create index...) 角色:一种特定的用户,包含已被赋予的权限

自定义角色,预定义角色 eq: connect dba resource 创建用户 create user 用户名identified by 密码 创建的用户是没有任何权限的,需要给权限 赋予权限: grant connect to 用户名 grant select on emp to 用户名-----select*from 用户名.table; 权限维护grant select on 用户名.emp to 用户名with grant option grant connect to xiaoming with admin option 注:取消中间者权限后被授权者人不再有该权限 收回权限 revoke select on 用户名.emp from 用户名 修改密码 password 用户名 删除用户 drop user 用户名 如果要删除的用户已经创建了表,那么删除是要带一个参数cascade 使用profile管理用户口令 creatr profile 配置文件名limit failed_login_attempts 3 password_lock_time 2; alter user 用户名profile 配置文件名; 解锁 alter user 用户名account unlock; 密码终止口令(可让用户定期修改密码) create profile 配置文件名limit password_life_time 10 password_grace_time 2; alter user 用户名profile 配置文件名 口令历史 建立PROFILE create profile password_history limit password_life_time 10 password_grace_time 2 password_reuse_time 10

相关主题
文本预览
相关文档 最新文档