日期:2014-05-16  浏览次数:20479 次

oracle的主键约束添加删除 (转)

5、创建外键约束
ALTER TABLE students
ADD CONSTRAINT fk_students_state
FOREIGN KEY (state) REFERENCES state_lookup (state);
6. 约束..
Alter table table_name add constrants BID primary key (bookno);
ALERT TABLE table_name MODIFY( column1 PRIMARY KEY);
1、创建表的同时创建主键约束
(1)无命名 create table student (studentid int primary key not null,
studentname varchar(8),age int);
(2)有命名 create table students (studentid int ,studentname varchar(8),
age int,constraint yy primary key(studentid));
2、删除表中已有的主键约束
(1)有命名 alter table students drop constraint yy;
(2)无命名 可用 SELECT * from user_cons_columns where ..;
查找表中主键名称得student表中的主键名为SYS_C002715
alter table student drop constraint SYS_C002715;
  (3) 使约束失效:
alter table tbl_employee disable constraint fk_emp;
删除约束:
alter table tbl_department drop constraint pk_dept;
查询约束:
select CONSTRAINT_NAME from user_constraints where table_name='TBL_EMPLOYEE';
select CONSTRAINT_NAME,COLUMN_NAME from user_cons_columns where table_name='TBL_EMPLOYEE';
3. 删除表.
     Drop table table_name;
4、操作表数据
插入表记录:
①. a. insert into table_name col1,col2 values (val1,val2);
例:Insrt into xs(xh,xm,) values (‘09’,to_date(‘19860210’,’yyyymmdd’));
insert into depto values('100','xieyunchao','m','22',to_date('19861104','yyyy-mm-dd'),10000)
b.从一个表中向另一个表中插入数据
             Insert into table1(col1,col2,col3) select (col1,col2,col3)
                   from othertable
c.使用子查询插入数据
  insert into employee (empno,ename,sal,deptno)
select empno,ename,sal,depto from emp;
d.INSERT INTO EMP (ENAME,HIREDATE) VALUE(‘AA’,TO_DATE(‘1980-12-09’,’YYYY-MM-DD’))
      ③. 删除表数据:
         Delete from table_name where condition;
      ④. 修改表记录
         Update table_name set column_name=expression,…where condition.
基于一张表修改另一张表的数据
UPDATE EMPLEE SET DEPTNO=(SELECT DENPNO FROM EMP WHERE EMPNO=7788)
WHERE JOB=(SELECT JOB FROM EMP WHERE EMPNO=7788)
      ⑤. 删除所有记录但保留表结构.
         Truncate table table_name;
      ⑥.查询数据
查询表结构:DESC table_name
         Select ename,sal,12*sal+100 from emp
注:select count(dinstinct(deptno)) from emp
a.查询大于平均的:
select empno from emp a,(select avg(sal) as sal_sal from emp) b
where a.sal>b.sal_sal;
       如果列中有空值时,则结果也为空(关于null值的处理(p47)).
         如:select ename,name,12*sal+comm如果comm的值为null,结果也为null;
如上所示,comm为null时.则12*sal+comm也为null;解决方法是用nvl方法替换.
在两个表中查询:以下两种方式都是一样的.
a.select t_phone_operation.operation_name
from t_phone_operation ,t_phone_operation_charge
where t_phone_operation.operation_id=t_phone_operation_charge.operation_id and
t_phone_operation_charge.phone_num=’159..’;
b.select operation_name from t_phone_operation where operation_id in (select operation_id from t_phone_operation_charge where phone_num='159...'
使用日期格式显示日期:
select ename,to_char(hiredate,’yyyy—mm---dd’) from emp
select ename where hiredate>to_date(‘1999-12-31’,’yyyy-mm-dd’);


使用别名的三种方式:
a. select ename as name,sal salary,from emp
b. select ename ” name”, sal*12 ”annual salary”
使用连接操作符:
Select ename || job as “employees” from emp
用连接字符:
Select ename ||’ ’||’ is a ’||’ ’||job as “employee details”
限制重复的行:
Select distinct deptno from emp
注意大小写:
Select ename,job,deptno from emp where job=’CLERK’
使用between ….and 运算符
Select ename,sal from emp where sal between 1000 and 1500;
使用in 运算符
Select empno,ename,sal,mgr from emp where mgr in(23,231,2345);
Like运算符:(模糊查询)
%代表至多任意字符
  _代表一个任意字符
如:select ename from emp where ename like ’s%’;
显示第三个字符为大写A的所有信息
SELECT ENAME ,SAL FROM EMP WHERE ENAME LIKE '__A%';
显示雇员名包含"_"的雇员信息(其中ESCAPE后的字符a为转义字符)
SELECT ENAME,SAL WHERE ENAME LIKE '%a_% ESCAPE 'a';
Null运算符(关于null值的处理(p47):)
  测试一个值是否为空:
Sel