SQL - Constraints • Check


--====================================================
SQL - Constraints      Check
Column Level Constarints / Table Level Constarints
--====================================================

DROP TABLE XX_EMP_TL

CREATE TABLE XX_EMP_TL
(
  EMPNO     NUMBER(4),
  ENAME     VARCHAR2(100),
  HIREDATE  DATE,
  DOB DATE,
  GENDER CHAR(1) Constraint GENDER_CK CHECK ( GENDER in ('M', 'F') ), -- Column Level Constraints -- 
  SAL       NUMBER(7),
  DEPTNO    NUMBER(2),
  CREATION_DATE Date Default sysdate,  
  Constraint XX_SAL CHECK ( SAL > 10000) -- Table Level Constraints -- 
)

select * from XX_EMP_TL

insert into  XX_EMP_TL (EMPNO, ENAME, HIREDATE, DOB, GENDER, SAL, DEPTNO, CREATION_DATE)  Values ( 1000, 'Sachin','11-SEP-1985', '1-MAR-2020', 'X', 15000, 10, sysdate)

insert into  XX_EMP_TL (EMPNO, ENAME, HIREDATE, DOB, GENDER, SAL, DEPTNO, CREATION_DATE)  Values ( 1000, 'Sachin','11-SEP-1985', '1-MAR-2020', 'M', 15000, 10, sysdate)

COMMIT;

select * from XX_EMP_TL

insert into  XX_EMP_TL (EMPNO, ENAME, HIREDATE, DOB, GENDER, SAL, DEPTNO, CREATION_DATE)  Values ( 1000, 'Sachin','11-SEP-1985', '1-MAR-2020', '', 25000, 10, sysdate)

Commit;

EMPNO
ENAME
HIREDATE
DOB
GENDER
SAL
DEPTNO
CREATION_DATE
1000
Sachin
11-Sep-85
01-Mar-20
M
15000
10
27-03-2020 12:56
1002
Sachin
11-Sep-85
01-Mar-20
25000
10
27-03-2020 12:57


ALTER TABLE XX_EMP_TL MODIFY (GENDER  NOT NULL)

UPDATE XX_EMP_TL set GENDER = 'M' where GENDER is null

COMMIT;

select * from XX_EMP_TL

insert into  XX_EMP_TL (EMPNO, ENAME, HIREDATE, DOB, GENDER, SAL, DEPTNO, CREATION_DATE)  Values ( 1000, 'Sachin','11-SEP-1985', '1-MAR-2020', '', 25000, 10, sysdate)


ALTER TABLE XX_EMP_TL ADD Constraint DEPT_CK CHECK ( DEPTNO = 10) 


insert into  XX_EMP_TL (EMPNO, ENAME, HIREDATE, DOB, GENDER, SAL, DEPTNO, CREATION_DATE)  Values ( 1003, 'Ravi','11-SEP-1985', '1-MAR-2020', 'M', 35000, 20, sysdate)

insert into  XX_EMP_TL (EMPNO, ENAME, HIREDATE, DOB, GENDER, SAL, DEPTNO, CREATION_DATE)  Values ( 1003, 'Ravi','11-SEP-1985', '1-MAR-2020', 'M', 35000, 10, sysdate)

Commit;

select * from XX_EMP_TL

EMPNO
ENAME
HIREDATE
DOB
GENDER
SAL
DEPTNO
CREATION_DATE
1000
Sachin
11-Sep-85
01-Mar-20
M
15000
10
27-03-2020 12:56
1000
Sachin
11-Sep-85
01-Mar-20
M
25000
10
27-03-2020 12:57
1003
Ravi
11-Sep-85
01-Mar-20
M
35000
10
27-03-2020 13:00


DROP TABLE  APPS.XX_EMP_TL

--=============


SELECT * FROM ALL_CONSTRAINTS 
WHERE TABLE_NAME = 'XX_EMP_TL'

--=============

CONSTRAINT_NAME
CONSTRAINT_TYPE
TABLE_NAME
SEARCH_CONDITION_VC
GENDER_CK
C
XX_EMP_TL
 GENDER in ('M', 'F')
XX_SAL
C
XX_EMP_TL
 SAL > 10000
SYS_C005381499
C
XX_EMP_TL
GENDER IS NOT NULL
DEPT_CK
C
XX_EMP_TL
 DEPTNO = 10


Comments

Popular posts from this blog

E-Text Report In Fusion | Types of E-Text reports

Supplier API's

How to pass default Parameter in cursor