东莞做网站一般多少钱,百度网站地图怎么做,网站中的文章可以做排名吗,一键制作网站ORACLE支持五种类型的完整性约束NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值.CHECK (检查)--检查在约束中指定的条件是否得到了满足.UNIQUE (唯一)--保证在指定的列中没有重复值.在该表中每一个值或者每一组值都将是唯…ORACLE支持五种类型的完整性约束NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值.CHECK (检查)--检查在约束中指定的条件是否得到了满足.UNIQUE (唯一)--保证在指定的列中没有重复值.在该表中每一个值或者每一组值都将是唯一的.PRIMARY KEY (主键)--用来唯一的标识出表的每一行,并且防止出现NULL值,一个表只能有一个主键约束.POREIGN KEY (外部键)--通过使用公共列在表之间建立一种父子(parent-child)关系,在表上定义的外部键可以指向主键或者其他表的唯一键.ORACLE支持五种类型的完整性约束NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值.CHECK (检查)--检查在约束中指定的条件是否得到了满足.UNIQUE (唯一)--保证在指定的列中没有重复值.在该表中每一个值或者每一组值都将是唯一的.PRIMARY KEY (主键)--用来唯一的标识出表的每一行,并且防止出现NULL值,一个表只能有一个主键约束.POREIGN KEY (外部键)--通过使用公共列在表之间建立一种父子(parent-child)关系,在表上定义的外部键可以指向主键或者其他表的唯一键.1--设置每行显示多少字符 set linesize 300;2 设置每页显示多少条记录 set pagesize 30;3 用户名的切换: 如 conn system/tigerConn sys/change_on_install as sysdba(注意超级用户 在后面加as sysdba)4 在超级用户下查找普通用户的表是查不到的 必须这样查找 如 select * from scott.emp(普通用户下的emp表)5 查看当前是那个用户身份登录: show user;6 查看有多少张表: select * from tab;(注意不同用户下的表是不同的)7查看表的结构: desc emp(emp为表名)8 取出重复的列(DISTINCT): 如 SELECT DISTINCT JOB EMP(去掉job的重复的值)9字符串的链接操作用: ||10 查询有奖金的员工: select* from emp where comm is not null;11 查询没有奖金的员工信息: select * from emp where comm is null;12 两个条件以上就得用and 如查询工资大雨1500和有奖金的员工 select * from emp where sal1500 and comm is not null;13 表示两个条件有一个满足就可就用:or 如查询工资大于1500或者没有奖金的员工信息Select * from emp where sal1500 or comm is not null;14取反可以用not 如 查询员工工资不大于1500和有奖金的员工信息 如:Select * from emp where not (sal1500 or comm is not null);15 在什么什么之间用between----and----如查询工资在1500和3000之间的员工信息:Select * from emp where sal between 1500 and 3000;16 查询员工编号是2323, 4555, 2222的员工具体信息: 如Select * from emp where empno in(2323,4555,2222);17.l模糊查询 like 一般结合%和_使用其中%:表示可以匹配任意长度的内容,_表示匹配一个长度放入内容 如: 查询员工姓名中第二哥字母是M的员工信息:Select * from emp where ename LIKE _M%;又如姓名中包含M的员工 Select * from emp where ename LIKE %M%;18oracle中不等于有两种表示方式和!19 排序用order by {asc desc}其中asc 是升序排列 如果不写就默认按升序排列desc是按降序排列 排序语句放在sal语句的最后如: 按员工工资进行排序Select * from emp order by sal asc(升序)Selecct * from emp order by sal desc(降序)Select * from emp where deptno10 order by sal desc,hiredate asc;(查询部门10的员工工资的升序排列如果工资相等就按员工的入职时间排序)20.group by 用于对查询的结果进行分组统计: 显示每个部门的平均工资和最高工资 如:Select avg(sal),max(sal) from emp group by deptno;Having 子句用于限制分组显示结果: 显示平均工资大于2000的的部门号和他的平均工资?如:select avg(sal), deptno from emp group by deptno having avg(sal)2000;2. 单行函数:1 小写变大写: upper 如 select * from emp where enameupper(smith);讲一个字符串变为小写字母表示 如: select lower(HELLO WORLD) FROM DUAL;将单词的首字母变大写 用 INITCAP 如: SELECT INITCAP(HELLO WORLD) FROM DUAL;2.字符串的操作Substr()截取字符串 length()字符串的长度 replace()替换字符串3数值函数四舍五入: round();截断小数位:trunc();一入门部分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_catalog_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 lihua.user_tbl to scott;--系统管理员12.修改表的结构(alter)Alter table 表名 add(列的名称,列的类型);二SQL查询和SQL函数1.SQl支持的命令 数据定义语言DDL:create,alter,drop 数据操纵语言DML:insert,delete,update,select 数据控制语言DCL:grant,revoke 事务控制语言TCL:commit,savepoint,rollback2.Oracle数据类型 字符数值日期RAWLOB 字符型char:1-2000字节的定长字符 varchar2:1-4000字节的变长字符 long:2GB的变长字符 注意一个表中最多可有一列为long型Long列不能定义唯一约束或主键约束long列上不能创建索引过程或存储过程不能接受long类型的参数。数值型number:最高精度38位 日期时间型 date:精确到ss timestamp:秒值精确到小数点后6位函数sysdate,systimestamp返回系统当前日期时间和时区。 更改时间的显示 alter session set nls_date_language’american’; alter session set nls_date_format’yyyy-mm-dd’;Oracle中的伪列 像一个表列但没有存储在表中伪列可以查询但不能插入、更新和修改它们的值 常用的伪列rowid和rownum rowid:表中行的存储地址可唯一标示数据库中的某一行可以使用该列快速定位表中的行。 rownum:查询返回结果集中的行的序号可以使用它来限制查询返回的行数。3.数据定义语言用于操作表的命令 create table alter table truncate table drop table修改表的命令 alter table stu_table rename to stu_tbl;--修改表名 alter table stu_tbl rename column stu_sex to sex;--修改列名 alter table stu_tbl add (stu_age number);--添加新列 alter table stu_tbl drop(sex);--删除列 alter table stu_tbl modify(stu_sex varchar2(2));--更改列的数据类型 alter table stu_tbl add constraint pk_stu_tbl primary key(id);--添加约束4.数据操纵语言select,update,delete,insert 利用现有的表创建表 create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;-- 选择无重复的行select distinct stu_name from stu_tbl;-- 插入来自其他表中的记录 insert into stu_tbl_log select id,stu_name,stu_age from stu_tbl;5.数据控制语言 grant,revoke 6.事务控制语言 commit,savepoint,rollback 7.SQL操作符 算术操作符:L-*/ 比较操作符:L,!,,,,,,between-and,in,like,is null等 逻辑操作符:Land,or,not 集合操作符:Lunion,union all,intersect,minus 连接操作符:L|| 示例中stu_tbl_log中的数据如下 ID STU_NAME STU_AGE---------- -------------------- ----------1000 李华 201001 accp 201003 nimda 3 stu_tbl中的数据如下 ID STU_NAME ST STU_AGE---------- -------------------- -- ----------1000 李华 男 201001 accp 男 201002 admin 男 30 示例 select (32)/2 from dual;--算术操作符结果2.5 select * from stu_tbl where stu_age20;--比较操作符 select * from stu_tbl where stu_name like %a%;--比较操作符like select * from stu_tbl where stu_name like a___;--比较操作符like select * from stu_tbl where stu_age in(20,30);--比较操作符in select * from stu_tbl where stu_age between 20 and 30;--比较操作符between select stu_name from stu_tbl union all select stu_name from stu_tbl_log;--集合操作符union all测试结果具体如下 STU_NAME-----------李华accpadmin李华accpnimda已选择6行。 select stu_name from stu_tbl union select stu_name from stu_tbl_log;--集合操作符union测试结果具体如下 STU_NAME---------accpadminnimda李华 select stu_name from stu_tbl intersect select stu_name from stu_tbl_log;--集合操作符intersect测试结具体如下 STU_NAME----------accp李华 select stu_name from stu_tbl minus select stu_name from stu_tbl_log;--集合操作符minus测试结果如下 STU_NAME----------Admin 从中可以看出minus是获取第一张表独有的数据 intersect是获取两张表中都有的数据 union是整合两张表的数据都有的只显示一次 union all是纯粹的两张表数据整合 select id,stu_name|| ||stu_sex as name_sex,stu_age from stu_tbl;--连接操作符||测试结果具体如下 ID NAME_SEX STU_AGE---------- ----------------------- ----------1000 李华 男 201001 accp 男 201002 admin 男 308.SQL函数 单行函数从表中查询的每一行只返回一个值可出现在select子句where子句中日期函数数字函数字符函数转换函数ToChar(),ToDate(),ToNumber()其他函数:Nvl(exp1,exp2):表达式一为null时返回表达式二Nvl2(exp1,exp2,exp3):表达式一为null时返回表达式三否则返回表达式二Nullif(exp1,exp2):两表达式相等时返回null否则返回表达式一 分组函数基于一组行来返回Avg,Min,Max,Sum,CountGroup by,having 分析函数Row_number,rank,dense_rank 示例 select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbloi,user_tbl u,order_tbl o where oi.order_id o.id and o.user_id u.id group by u.user_name;三锁和数据库对象1.锁数据库用来控制共享资源并发访问的机制。 锁的类型行级锁表级锁 行级锁对正在被修改的行进行锁定。行级锁也被称之为排他锁。 在使用下列语句时Oracle会自动应用行级锁 insert,update,delete,select…… for update select……for update允许用户一次锁定多条记录进行更新。 使用commit or rollback释放锁。 表级锁 lock table user_tbl in mode mode; 表级锁类型 行共享 row share 行排他 row exclusive 共享 share 共享行排他 share row exclusive 排他 exclusive 死锁两个或两个以上的事务相互等待对方释放资源从而形成死锁 2.数据库对象 oracle数据库对象又称模式对象 数据库对象是逻辑结构的集合最基本的数据库对象是表 数据库对象表序列视图索引
序列用于生成唯一连续序号的对象。 创建语法 create sequence user_id_seq start with 1000 increment by 1 maxvalue 2000 minvalue 1000 nocycle cache 1000;--指定内存中预先分配的序号 访问序列 select user_id_seq.currval from dual; select user_id-seq.nextval from dual;更改删除序列 alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值 drop sequence user_id_seq; 在Hibernate中访问序列user_id_seq视图以经过定制的方式显示来自一个或多个表的数据 创建视图 create or replace view user_tbl_view (vid,vname,vage) as select id,user_name,age from user_tbl [with check option]|[with read only]; 创建带有错误的视图 create force view user_tbl_force_view as select * from user_table;--此时user_table可以不存在 创建外联接视图 create view user_stu_view as select u.id,u.user_name,u.password,s.ddress from user_tbl u,stu_tbl s where u.s_id()s.id;--哪一方带有()哪一方就是次要的 删除视图 drop user_stu_view;索引 用于提高SQL语句执行的性能 索引类型 唯一索引位图索引组合索引基于函数的索引反向键索引创建标准索引 create index user_id_index on user_tbl(id) tablespace schooltbs; 重建索引 alter index user_id_index rebuild; 删除索引 drop index user_id_index;创建唯一索引 create unique index user_id_index on user_tbl(id); 创建组合索引 create index name_pass_index on user_tbl(user_name,password); 创建反向键索引 create index user_id_index on user_tbl(id) reverse;四使用PL/SQL可用于创建存储过程触发器程序包给SQL语句的执行添加程序逻辑。 支持SQL在PL/SQL中可以使用数据操纵命令 事务控制命令 游标控制 SQL函数和SQL运算符支持面向对象编程(OOP)可移植性更佳的性能PL/SQL经过编译执行分为三个部分声明部分可执行部分和异常处理部分 [declare declarations] begin executable statements [exception handlers] end;打开输出 set serverout on;--根据输入编号获取某学员的成绩--ifdeclarescore user_tbl.score%type;beginselect score into score from user_tbl where idid;if score90 thendbms_output.put_line(优秀);elsif score80 thendbms_output.put_line(良好);elsif score60 thendbms_output.put_line(及格);elsedbms_output.put_line(差);end if;end;--根据学员姓名获取某学员的成绩--ifdeclarescore user_tbl.score%type;beginselect score into score from user_tbl where user_namename;if score90 thendbms_output.put_line(优秀);elsif score80 thendbms_output.put_line(良好);elsif score60 thendbms_output.put_line(及格);elsedbms_output.put_line(差);end if;end;--case的使用declaregrade user_tbl.grade%type;beginselect grade into grade from user_tbl where idid;case gradewhen A then dbms_output.put_line(优异);when B then dbms_output.put_line(优秀);when C then dbms_output.put_line(良好);else dbms_output.put_line(一般);end case;end;--基本循环declarei number(4):1;beginloopdbms_output.put_line(loop size:||i);i:i1;exit when i10;end loop;end;--while循环declarei number(4):1;beginwhile i10 loopdbms_output.put_line(while loop size||i);i:i1;end loop;end;--for循环declarei number(4):1;beginfor i in 1..10 loopdbms_output.put_line(for loop Size:||i);end loop;end;declarei number(2):1;j number(2):1;beginfor i in reverse 1..9 loopfor j in 1..i loopdbms_output.put(j||x||i||||j*i|| );end loop;dbms_output.put_line();end loop;end;--动态SQLdeclareuserId number(2);sql_str varchar2(100);userName user_tbl.user_name%type;beginexecute immediate create table testExe(id number,test_name varchar2(20));userId:userId;sql_str:select user_name from user_tbl where id:id;execute immediate sql_str into userName using userId;dbms_output.put_line(userName);end;(ordeclareid_param number:id_param;sql_str varchar2(100);name_param stu_tbl.stu_name%type;beginsql_str:select stu_name from stu_tbl where id:p;execute immediate sql_str into name_param using id_param;dbms_output.put_line(name_param);end;/)--异常处理declaregrade number(4);begingrade:grade;case gradewhen 1 then dbms_output.put_line(好的);--else dbms_output.put_line(不好);end case;exceptionwhen case_not_found thendbms_output.put_line(输入类型不匹配);end;--系统异常declarerowD user_tbl%rowtype;beginselect * into rowD from user_tbl;dbms_output.put_line(rowD.id||||rowD.user_name|| ||rowD.password);exceptionwhen too_many_rows thendbms_output.put_line(不能将多行赋予一个属性);end;ordeclarerowD user_tbl%rowtype;beginselect * into rowD from user_tbl where id5;dbms_output.put_line(rowD.id|| ||rowD.user_name|| ||rowD.password);exceptionwhen too_many_rows thendbms_output.put_line(不能将多行赋予一个属性);when no_data_found thendbms_output.put_line(没有您要查找的数据);end;--自定义错误declareinvalidError exception;category varchar2(20);begincategory:category;if category not in(附件,顶盘,备件) thenraise invalidError;elsedbms_output.put_line(您输入的类别是||category);end if;exceptionwhen invalidError thendbms_output.put_line(无法识别的类别);end;--引发应用程序异常declareapp_exception exception;grade user_tbl.grade%type;beginselect grade into grade from user_tbl where idid;if gradeA thenraise app_exception;elsedbms_output.put_line(查询的等级为||grade);end if;exceptionwhen app_exception thenraise_application_error(-20001,未知的等级);end;五、游标管理游标类型隐式游标显式游标REF游标REF游标用于处理运行时才能确定的动态SQL查询的结果隐式游标在PL/SQL中使用DML语句时自动创建隐式游标 隐式游标自动声明、打开和关闭其名为SQL隐式游标的属性 %found SQL语句影响实质后返回true %notfound SQL语句没有影响实质后返回true %rowcount SQL语句影响的行数 %isopen 游标是否打开始终为false示例beginupdate user_tbl set scorescore5;if SQL%found thendbms_output.put_line(数据被更改 ||SQL%rowcount);elsif sql%notfound thendbms_output.put_line(没有找到数据);end if;if SQL%isopen thendbms_output.put_line(Open);elsedbms_output.put_line(Close);end if;end;显式游标在PL/SQL的声明部分定义查询该查询可以返回多行J 声明游标J 打开游标J 从游标中取回数据J 关闭游标声明游标完成两个任务给游标命名将一个查询与游标关联 cursor cursor_name is select statement;打开游标open cursor_name;取数据fetch cursor_name into record_list;关闭游标close cursor_name;显式游标的属性%found 执行最后一条fetch语句成功返回行时为true%notfound 执行最后一条fetch语句未能返回行时为true%rowcount 返回到目前为止游标提取的行数%isopen 游标是否打开示例declareusers user_tbl%rowtype;cursor boys_cur is select * from user_tbl where sexh;beginopen boys_cur;loopfetch boys_cur into users;exit when boys_cur%notfound;dbms_output.put_line(users.user_name|| ||users.password);dbms_output.put_line(boys_cur%rowcount);end loop;close boys_cur;end;带参的显式游标declareusers user_tbl%rowtype;cursor boys_cur(sexParam varchar2)is select * from user_tbl where sexsexParam;beginopen boys_cur(sex);loopfetch boys_cur into users;exit when boys_cur%notfound;dbms_output.put_line(users.user_name|| ||users.password);dbms_output.put_line(boys_cur%rowcount);end loop;close boys_cur;end;使用显式游标更新行declarecursor user_update_cur is select sex from user_tbl for update;usersex user_tbl.sex%type;beginopen user_update_cur;loopfetch user_update_cur into usersex;exit when user_update_cur%notfound;dbms_output.put_line(usersex);if usersex M thenupdate user_tbl set scorescore-5 where current of user_update_cur;elseupdate user_tbl set scorescore5 where current of user_update_cur;end if;end loop;close user_update_cur;commit;end;循环游标declarecursor user_cur is select * from user_tbl;beginfor username in user_cur loopdbms_output.put_line(username.user_name|| ||username.sex);end loop;end;REF游标REF游标和游标变量用于处理运行时动态执行的SQL查询创建游标变量的步骤J 声明REF游标类型J 声明REF游标类型的变量声明类型的语法Type ref_cursor_name is ref cursor [return return_type];打开游标变量的语法Open cursor_name for select_statement;----声明强类型的游标declaretype ref_cur is ref cursor return user_tbl%rowtype;users_cur ref_cur;----声明弱类型的游标declaretype ref_cur is ref cursor;users_cur ref_cur;示例----强类型declaretype ref_cur is ref cursor return user_tbl%rowtype;users_cur ref_cur;users user_tbl%rowtype;beginopen users_cur for select * from user_tbl where user_nameny2t92;loopfetch users_cur into users;exit when users_cur%notfound;dbms_output.put_line(users.user_Name);end loop;close users_cur;end;----弱类型declaretype ref_cur is ref cursor;my_cur ref_cur;users user_tbl%rowtype;stus stu_tbl%rowtype;beginopen my_cur for select * from user_tbl;loopfetch my_cur into users;exit when my_cur%notfound;dbms_output.put_line(users.user_Name);end loop;close my_cur;open my_cur for select * from user_tbl where user_nameny2t92;loopfetch my_cur into users;exit when my_cur%notfound;dbms_output.put_line(users.user_Name);end loop;close my_cur;open my_cur for select * from stu_tbl;loopfetch my_cur into stus;exit when my_cur%notfound;dbms_output.put_line(stus.stu_Name);end loop;close my_cur;end;----动态SQL游标declaretype ref_cur is ref cursor;my_cur ref_cur;users user_tbl%rowtype;username varchar2(20);sqlstmt varchar2(200);beginusername:username;sqlstmt : select * from user_tbl where user_name :name;open my_cur for sqlstmt using username;loopfetch my_cur into users;exit when my_cur%notfound;dbms_output.put_line(users.user_Name);end loop;close my_cur;end;六子程序子程序分为存储过程和函数它是命名的PL/SQL块编译并存储在数据库中。子程序的各个部分声明部分可执行部分异常处理部分。过程----执行某些操作函数----执行操作并返回值存储过程创建过程的语法create or replace procedureproce_name (parameter_list)is|aslocal variable declarationbeginexecutable statementsexceptionexception_handlersend proce_name;过程参数的三种模式In----用于接收调用的值默认的参数模式Out----用于向调用程序返回值In out----用于接收调用程序的值并向调用程序返回更新的值执行过程的语法Execute proce_name(parameter_list);或DeclareVariable var_list;BeginProce_name(var_list);End;将过程执行的权限授予其他用户Grant execute on proce_name to scott;Grant execute on proce_name to public;删除存储过程Drop procedure proce_name;函数创建函数的语法Create or replace functionFun_name (parameter_list)Return datatype is|asLocal declarationsBeginExecutable statements;Return result;ExceptionExce_handlers;End;函数只能接收in参数不能接受out或in out参数形参不能是PL/SQL类型函数的返回类型也必须是数据库类型访问函数的方式J 使用PL/SQL块J 使用SQL语句Select fun_name(parameter_list) from dual;