18, నవంబర్ 2013, సోమవారం

A class room explanations about integrity constraints in oracle from




   Mr ramu dayinaboyina dept of computing                                                    page 1/2
*      Create table emp1(empno number(5) not null , ename varchar2(15) );
*      Alter table emp1 modify(ename varchar2(15) not null);
*      Alter table emp1 modify(ename varchar2(15) null);

*      Create table emp1(empno number(5) not null , ename varchar2(15), doj date default sysdate );


*      Create table dept1(deptno number constraint check_deptno  check(deptno between 10 and 99),dname varchar2(9) constraint check_dname  check(dname=upper(dname)), location varchar2(10) constraint check_loc check(loc in(‘dallas’,’bostan’,’newyork’,’chicago’)));

*      Create table dept1(deptno number(2) constraint unique_dname unique , dname varchar2(20));
*      Alter table dept1 add constraint unq1 unique(dname);
*      Alter table dept1 drop constraint unq1 ;

*      Create table dept1(deptno number(2), dname varchar2(20), loc varchar2(10),   constraint unique_dname unique(deptno , dname ));


*      Create table dept1(deptno number(2) constraint pk_dept primary key , dname varchar2(20));
SQL CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
SQL CHECK Constraint on CREATE TABLE
The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0.
SQL Server / Oracle / MS Access:    CREATE TABLE Persons
(   P_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),Address varchar(255),  City varchar(255)
)

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:                                                                    page 2/2
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
)
SQL CHECK Constraint on ALTER TABLE
To create a CHECK constraint on the "P_Id" column when the table is already created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons ADD CHECK (P_Id>0)
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons  ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')

To DROP a CHECK Constraint  To drop a CHECK constraint, use the following SQL:
SQL Server / Oracle / MS Access:
ALTER TABLE Persons  DROP CONSTRAINT chk_Person
MySQL:
ALTER TABLE Persons  DROP CHECK chk_Person



కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి