Oracle PL/SQL Tutorial/System Tables Data Dictionary/user constraints — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:08, 26 мая 2010
Содержание
Getting Information on Constraints
You can get information on your constraints by querying user_constraints.
SQL> desc user_constraints;
Name Null? Type
--------------------
OWNER NOT NULL VARCHAR2(30)
CONSTRAINT_NAME NOT NULL VARCHAR2(30)
CONSTRAINT_TYPE VARCHAR2(1) --Constraint type. Set to P, R, C, U, V, or O.
TABLE_NAME NOT NULL VARCHAR2(30)
SEARCH_CONDITION LONG
R_OWNER VARCHAR2(30)
R_CONSTRAINT_NAME VARCHAR2(30)
DELETE_RULE VARCHAR2(9)
STATUS VARCHAR2(8) --Constraint status. Set to ENABLED or DISABLED.
DEFERRABLE VARCHAR2(14) --Whether the constraint is deferrable. Set to DEFERRABLE or NOT DEFERRABLE.
DEFERRED VARCHAR2(9) --Whether the deferred. Set to IMMEDIATE or DEFERRED.
VALIDATED VARCHAR2(13)
GENERATED VARCHAR2(14)
BAD VARCHAR2(3)
RELY VARCHAR2(4)
LAST_CHANGE DATE
INDEX_OWNER VARCHAR2(30)
INDEX_NAME VARCHAR2(30)
INVALID VARCHAR2(7)
VIEW_RELATED VARCHAR2(14)
Getting Information on View Constraints
You get information on view constraints from user_constraints
SQL> desc user_constraints;
Name Null? Type
------------------------
OWNER NOT NULL VARCHAR2(30)
CONSTRAINT_NAME NOT NULL VARCHAR2(30)
CONSTRAINT_TYPE VARCHAR2(1)
TABLE_NAME NOT NULL VARCHAR2(30)
SEARCH_CONDITION LONG
R_OWNER VARCHAR2(30)
R_CONSTRAINT_NAME VARCHAR2(30)
DELETE_RULE VARCHAR2(9)
STATUS VARCHAR2(8)
DEFERRABLE VARCHAR2(14)
DEFERRED VARCHAR2(9)
VALIDATED VARCHAR2(13)
GENERATED VARCHAR2(14)
BAD VARCHAR2(3)
RELY VARCHAR2(4)
LAST_CHANGE DATE
INDEX_OWNER VARCHAR2(30)
INDEX_NAME VARCHAR2(30)
INVALID VARCHAR2(7)
VIEW_RELATED VARCHAR2(14)
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL> CREATE VIEW myView AS
2 SELECT *
3 FROM employee
4 WITH READ ONLY CONSTRAINT my_view_read_only;
View created.
SQL>
SQL> SELECT
2 constraint_name, constraint_type, status, deferrable, deferred
3 FROM user_constraints
4 WHERE table_name IN ("MYVIEW");
CONSTRAINT_NAME C STATUS DEFERRABLE DEFERRED
------------------------------ - -------- -------------- ---------
MY_VIEW_READ_ONLY O ENABLED NOT DEFERRABLE IMMEDIATE
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
Query table constraint type by querying user_constraints table
SQL> select table_name
2 , constraint_name
3 , case constraint_type
4 when "P" then "PRIKEY"
5 when "U" then "UNIQUE"
6 when "C" then " CHECK"
7 when "R" then "FORKEY"
8 else " OTHER"
9 end as type
10 , status
11 from user_constraints
12 order by table_name
13 , type;
TABLE_NAME CONSTRAINT_NAME TYPE STATUS
------------------------------ ------------------------------ ------ --------
AVAIL_TRACE_FILES AVAIL_TRACE_FILES_PK PRIKEY ENABLED
BIN$50P9fUVyS3CVhoJ8p86aww==$0 BIN$VMcdepmcR4ug/22rvF+1xw==$0 CHECK ENABLED
BIN$50P9fUVyS3CVhoJ8p86aww==$0 BIN$CiI/tY7QTzCYPzbRan2qug==$0 CHECK ENABLED
BIN$50P9fUVyS3CVhoJ8p86aww==$0 BIN$5CEHm8aoQI+QEJVhy5P0cQ==$0 CHECK ENABLED
BIN$50P9fUVyS3CVhoJ8p86aww==$0 BIN$Sub47Nl/TUy+yxXOAxEVEQ==$0 CHECK ENABLED
BIN$9NVb8X4RQD++Dre0uN5ukA==$0 BIN$uyol6urWTla+zIWnSplRmg==$0 CHECK ENABLED
BIN$9NVb8X4RQD++Dre0uN5ukA==$0 BIN$I9xRa3jJSBSAauBv1trS8g==$0 CHECK ENABLED
BIN$9NVb8X4RQD++Dre0uN5ukA==$0 BIN$wgvs7nkURFWztU+b57HYPQ==$0 CHECK ENABLED
BIN$9NVb8X4RQD++Dre0uN5ukA==$0 BIN$CtIz0hnsSXqaJPY8sw0+pQ==$0 CHECK ENABLED
BIN$9NVb8X4RQD++Dre0uN5ukA==$0 BIN$E2ZL/AWfSmCZCAacajsefw==$0 UNIQUE ENABLED
BIN$A2IWocWXS/mUCrAdQt9RoQ==$0 BIN$fYL/DxaDSy6P6z8F7l8h/w==$0 CHECK ENABLED
BIN$A2IWocWXS/mUCrAdQt9RoQ==$0 BIN$wyDlgiD/SrSprbYY1MWd1w==$0 CHECK ENABLED
BIN$A2IWocWXS/mUCrAdQt9RoQ==$0 BIN$dqIBTtLSTa+MD7AIx3qdPg==$0 CHECK ENABLED
BIN$A2IWocWXS/mUCrAdQt9RoQ==$0 BIN$NKGL/hLdS12D4ts14Ddtww==$0 CHECK ENABLED
BIN$cVJWgW8YSn6s3cAxBuliGQ==$0 BIN$rsD6uhRWQV6vdE3Z9UV2RA==$0 CHECK ENABLED
BIN$cVJWgW8YSn6s3cAxBuliGQ==$0 BIN$aL7CwqNTQHGbCUdghbAD1Q==$0 CHECK ENABLED
BIN$cVJWgW8YSn6s3cAxBuliGQ==$0 BIN$AfKMoPrLTt+44vfSMnIUGQ==$0 CHECK ENABLED
BIN$cVJWgW8YSn6s3cAxBuliGQ==$0 BIN$823WYDmCQ3iD6DihooQkPw==$0 CHECK ENABLED
BIN$cVJWgW8YSn6s3cAxBuliGQ==$0 BIN$hRwWn2gTQrGr8ley9ne59w==$0 PRIKEY ENABLED
BIN$qf21D14XRemOOpMbQAFcmA==$0 BIN$qv1rrdw5QruTMJ4OO+NEsg==$0 CHECK ENABLED
BIN$qf21D14XRemOOpMbQAFcmA==$0 BIN$Y1rnbAPqQhulusWZAN8UlA==$0 CHECK ENABLED
BIN$qf21D14XRemOOpMbQAFcmA==$0 BIN$T94dKQiqS2SwPcQ+SJCMVA==$0 CHECK ENABLED
BIN$qf21D14XRemOOpMbQAFcmA==$0 BIN$Z6p5I0ZiQAS94s0pILt7Uw==$0 CHECK ENABLED
DDL_AUDIT SYS_C004381 CHECK ENABLED
DDL_AUDIT SYS_C004378 CHECK ENABLED
DDL_AUDIT SYS_C004379 CHECK ENABLED
DDL_AUDIT SYS_C004380 CHECK ENABLED
HAZARD_LEVEL_T SYS_C004621 CHECK ENABLED
HAZARD_LEVEL_T SYS_C004617 CHECK ENABLED
HAZARD_LEVEL_T SYS_C004620 CHECK ENABLED
HAZARD_LEVEL_T SYS_C004619 CHECK ENABLED
HAZARD_LEVEL_T SYS_C004618 CHECK ENABLED
HOME_ADDRESS_LIST SYS_C004533 PRIKEY ENABLED
HOME_ADDRESS_LIST SYS_C004534 UNIQUE ENABLED
I1 SYS_C005790 PRIKEY ENABLED
I2 SYS_C005791 PRIKEY ENABLED
MAP SYS_C005793 FORKEY ENABLED
MAP SYS_C005794 FORKEY ENABLED
MAP SYS_C005792 PRIKEY ENABLED
P SYS_C006768 PRIKEY ENABLED
PUBLICATION PUBLICATION_PK PRIKEY ENABLED
SITE_TAB SYS_C004442 CHECK ENABLED
SITE_TAB SYS_C004441 CHECK ENABLED
WORKING_CUSTOMERS SYS_C006546 CHECK ENABLED
WORKING_CUSTOMERS SYS_C006545 CHECK ENABLED
45 rows selected.
SQL>
Query user_constraints for constraint_type = "V"
SQL>
SQL>
SQL> create table employees(
2 empno NUMBER(4)
3 , ename VARCHAR2(8)
4 , init VARCHAR2(5)
5 , job VARCHAR2(8)
6 , mgr NUMBER(4)
7 , bdate DATE
8 , msal NUMBER(6,2)
9 , comm NUMBER(6,2)
10 , deptno NUMBER(2) ) ;
Table created.
SQL>
SQL>
SQL> insert into employees values(1,"Jason", "N", "TRAINER", 2, date "1965-12-18", 800 , NULL, 10);
1 row created.
SQL> insert into employees values(2,"Jerry", "J", "SALESREP",3, date "1966-11-19", 1600, 300, 10);
1 row created.
SQL> insert into employees values(3,"Jord", "T" , "SALESREP",4, date "1967-10-21", 1700, 500, 20);
1 row created.
SQL> insert into employees values(4,"Mary", "J", "MANAGER", 5, date "1968-09-22", 1800, NULL, 20);
1 row created.
SQL> insert into employees values(5,"Joe", "P", "SALESREP",6, date "1969-08-23", 1900, 1400, 30);
1 row created.
SQL> insert into employees values(6,"Black", "R", "MANAGER", 7, date "1970-07-24", 2000, NULL, 30);
1 row created.
SQL> insert into employees values(7,"Red", "A", "MANAGER", 8, date "1971-06-25", 2100, NULL, 40);
1 row created.
SQL> insert into employees values(8,"White", "S", "TRAINER", 9, date "1972-05-26", 2200, NULL, 40);
1 row created.
SQL> insert into employees values(9,"Yellow", "C", "DIRECTOR",10, date "1973-04-27", 2300, NULL, 20);
1 row created.
SQL> insert into employees values(10,"Pink", "J", "SALESREP",null,date "1974-03-28", 2400, 0, 30);
1 row created.
SQL>
SQL>
SQL>
SQL> create or replace view dept20_v as
2 select * from employees where deptno=20 with check option;
View created.
SQL>
SQL> select constraint_name, table_name
2 from user_constraints
3 where constraint_type = "V";
CONSTRAINT_NAME TABLE_NAME
------------------------------ ------------------------------
SYS_C007091 DEPT20_V
1 row selected.
SQL>
SQL> drop table employees;
Table dropped.
SQL>
SQL>
Query user_constraints with constraint_type = "R"
SQL>
SQL>
SQL> create table registrations
2 ( attendee NUMBER(4)
3 , course VARCHAR2(6)
4 , begindate DATE
5 , evaluation NUMBER(1)) ;
Table created.
SQL>
SQL>
SQL> insert into registrations values (1, "SQL",date "1999-04-12",4 );
1 row created.
SQL> insert into registrations values (2, "SQL",date "1999-12-13",NULL);
1 row created.
SQL> insert into registrations values (3, "SQL",date "1999-12-13",NULL);
1 row created.
SQL> insert into registrations values (4, "OAU",date "1999-08-10",4 );
1 row created.
SQL> insert into registrations values (5, "OAU",date "2000-09-27",5 );
1 row created.
SQL> insert into registrations values (6, "JAV",date "1999-12-13",2 );
1 row created.
SQL> insert into registrations values (7, "JAV",date "2000-02-01",4 );
1 row created.
SQL> insert into registrations values (8, "JAV",date "2000-02-01",5 );
1 row created.
SQL> insert into registrations values (9, "XML",date "2000-02-03",4 );
1 row created.
SQL> insert into registrations values (10,"XML",date "2000-02-03",5 );
1 row created.
SQL> insert into registrations values (1, "PLS",date "2000-09-11",NULL);
1 row created.
SQL> insert into registrations values (2, "PLS",date "2000-09-11",NULL);
1 row created.
SQL> insert into registrations values (3, "PLS",date "2000-09-11",NULL);
1 row created.
SQL>
SQL>
SQL> alter table registrations
2 add (entered_by number(4) default 7839 not null);
Table altered.
SQL>
SQL> select table_name
2 , constraint_name
3 , status
4 , r_constraint_name as references
5 from user_constraints
6 where constraint_type = "R";
TABLE_NAME CONSTRAINT_NAME STATUS
------------------------------ ------------------------------ --------
REFERENCES
------------------------------
MAP SYS_C005794 ENABLED
SYS_C005791
MAP SYS_C005793 ENABLED
SYS_C005790
SQL>
SQL> drop table registrations;
Table dropped.