0% found this document useful (0 votes)
37 views18 pages

Dbms Constraints 1

Uploaded by

mikku ninavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views18 pages

Dbms Constraints 1

Uploaded by

mikku ninavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

sample_constraint

SQL> desc employee;


Name Null? Type
----------------------------------------- -------- ----------------------------

FNAME NOT NULL VARCHAR2(15)


MINIT CHAR(1)
LNAME NOT NULL VARCHAR2(15)
SSNN NOT NULL CHAR(9)
BDATE DATE
ADDRESS VARCHAR2(50)
SEX CHAR(1)
SUPERSSN CHAR(9)
DEPNO NUMBER
SALARY NUMBER(7)

SQL> alter table employee modify salary number(7) default 800;

Table altered.

SQL> commit;

Commit complete.

SQL> alter table employee add constraint emp_fsal foreign key


(superssn) referen
ces employee (ssnn) on delete set null;

Table altered.

SQL> desc department;


Page 1
sample_constraint
ERROR:
ORA-04043: object department does not exist

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BANKDETAILS TABLE
BIN$+Ckn0H2VTWe+9UdxY4fqLw==$0 TABLE
BIN$LueyI7A1QLKdm9SSMBFmaQ==$0 TABLE
BIN$ODLOM44GR127zLqGu0dcrw==$0 TABLE
BIN$YCTc+fHjS7Cr1IWomi41Yw==$0 TABLE
BIN$xbTjFI5xSKiy+Sh96icpuQ==$0 TABLE
CUST TABLE
DEPT TABLE
DEPT_LOCATIONS TABLE
DOCTOR TABLE
EMPLOYEE TABLE

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
PROJECT TABLE
RECO TABLE
VITDB TABLE

14 rows selected.

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------------------

DNAME VARCHAR2(15)
Page 2
sample_constraint
DEPNO NUMBER
MGRSSN CHAR(9)
MGRDATE DATE

SQL> alter table employee add constraint emp_fdepno foreign key


(depno) referenc
es dept (depno) on delete cascade;
alter table employee add constraint emp_fdepno foreign key
(depno) references de
pt (depno) on delete cascade

*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list

SQL> select * from dept;

DNAME DEPNO MGRSSN MGRDATE


--------------- ---------- --------- ---------
Manufacture 1 888665555 19-JUN-71
Administration 2 543216789 04-JAN-99
Headquarter 3 554433221 22-SEP-55
Finance 4 987654321 01-JAN-85
Research 5 333445555 22-MAY-78

SQL> select depno from employee;

DEPNO
----------
3
3
5
Page 3
sample_constraint
4
5
5
3
1
4
4

10 rows selected.

SQL> alter table employee add constraint emp_fdepno foreign key


(depno) referenc
es dept (depno) on delete cascade;
alter table employee add constraint emp_fdepno foreign key
(depno) references de
pt (depno) on delete cascade

*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list

SQL> desc employee;


Name Null? Type
----------------------------------------- -------- ----------------------------

FNAME NOT NULL VARCHAR2(15)


MINIT CHAR(1)
LNAME NOT NULL VARCHAR2(15)
SSNN NOT NULL CHAR(9)
BDATE DATE
ADDRESS VARCHAR2(50)
SEX CHAR(1)
Page 4
sample_constraint
SUPERSSN CHAR(9)
DEPNO NUMBER
SALARY NUMBER(7)

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------------------

DNAME VARCHAR2(15)
DEPNO NUMBER
MGRSSN CHAR(9)
MGRDATE DATE

SQL> slect * from dept;


SP2-0734: unknown command beginning "slect * fr..." - rest of line
ignored.
SQL> select * from dept;

DNAME DEPNO MGRSSN MGRDATE


--------------- ---------- --------- ---------
Manufacture 1 888665555 19-JUN-71
Administration 2 543216789 04-JAN-99
Headquarter 3 554433221 22-SEP-55
Finance 4 987654321 01-JAN-85
Research 5 333445555 22-MAY-78

SQL> alter table employee add constraint emp_fdepno foreign key


(depno) referenc
es dept (depno) on delete cascade;
alter table employee add constraint emp_fdepno foreign key
(depno) references de
pt (depno) on delete cascade

Page 5
sample_constraint
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list

SQL> alter table dept add constraint dept_pkey primary key


(depno);

Table altered.

SQL> alter table employee add constraint emp_fdepno foreign key


(depno) referenc
es dept (depno) on delete cascade;

Table altered.

SQL> commit;

Commit complete.

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------------------

DNAME VARCHAR2(15)
DEPNO NOT NULL NUMBER
MGRSSN CHAR(9)
MGRDATE DATE

SQL> alter table dept modify(dname varchar(15) constraint


dept_dname NOT NULL);

Table altered.
Page 6
sample_constraint

SQL> alter table dept add constraint dept_fmgrssn foreign key


(mgrssn) reference
s employee (ssnn) on delete set null;

Table altered.

SQL> select * from dept_locations;

DEPNO DLOCATION
---------- ---------------
1 Houston
1 Chicago
2 New York
2 San Francisco
3 Salt Lake City
4 Stafford
4 Bellaire
4 Sugarland
5 Houston

9 rows selected.

SQL> alter table dept_locations add constraint dept_fdepno


foreign key (depno) r
eferences dept (depno) on delete cascade;

Table altered.

SQL> commit;

Commit complete.

Page 7
sample_constraint
SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BANKDETAILS TABLE
BIN$+Ckn0H2VTWe+9UdxY4fqLw==$0 TABLE
BIN$LueyI7A1QLKdm9SSMBFmaQ==$0 TABLE
BIN$ODLOM44GR127zLqGu0dcrw==$0 TABLE
BIN$YCTc+fHjS7Cr1IWomi41Yw==$0 TABLE
BIN$xbTjFI5xSKiy+Sh96icpuQ==$0 TABLE
CUST TABLE
DEPT TABLE
DEPT_LOCATIONS TABLE
DOCTOR TABLE
EMPLOYEE TABLE

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
PROJECT TABLE
RECO TABLE
VITDB TABLE

14 rows selected.

SQL> desc project;


Name Null? Type
----------------------------------------- -------- ----------------------------

PNAME VARCHAR2(15)
PNUMBER NUMBER(5)
PLOCATION VARCHAR2(15)
DEPNO NUMBER(5)

Page 8
sample_constraint
SQL> alter table project modify(pname varchar2(15) constraint
proj_pname NOT NUL
L);

Table altered.

SQL> alter table project add constraint proj_pkey primary key


(pnumber);

Table altered.

SQL> alter table project add constraint proj_fdepno foreign key


(depno) referenc
es dept (depno) on delete set null;

Table altered.

SQL> commit;

Commit complete.

SQL> create table works_on (ssnn char(9), pno number(5), hrs


decimal(3,1) constr
aint hrs_con not null,constraint ssnn_fkey foreign key(ssnn)
references employee
(ssnn) on delete cascade, constraint pno_fkey foreign key(pno)
references projec
t(pnumber) on delete cascade);

Table created.

SQL> insert into works_on values('123456789', 3388, 32.5);

Page 9
sample_constraint
1 row created.

SQL> insert into works_on values('123456789', 1945, 7.5);

1 row created.

SQL> insert into works_on values('666884444', 3388, 40.0);

1 row created.

SQL> insert into works_on values('453453453', 7745, 20.0);

1 row created.

SQL> insert into works_on values('453453453', 2212, 20.0);

1 row created.

SQL> insert into works_on values('333445555', 7745, 10.0);

1 row created.

SQL> insert into works_on values('333445555', 6688, 10.0);

1 row created.

SQL> insert into works_on values('333445555', 4345, 35.0);

1 row created.

SQL> insert into works_on values('333445555', 2212, 28.5);

1 row created.
Page 10
sample_constraint

SQL> insert into works_on values('999887777', 2212, 11.5);

1 row created.

SQL> insert into works_on values('543216789', 2212, 17.0);

1 row created.

SQL> insert into works_on values('554433221', 1945, 21.5);

1 row created.

SQL> select * from works_on;

SSNN PNO HRS


--------- ---------- ----------
123456789 3388 32.5
123456789 1945 7.5
666884444 3388 40
453453453 7745 20
453453453 2212 20
333445555 7745 10
333445555 6688 10
333445555 4345 35
333445555 2212 28.5
999887777 2212 11.5
543216789 2212 17

SSNN PNO HRS


--------- ---------- ----------
554433221 1945 21.5

Page 11
sample_constraint
12 rows selected.

SQL> commit;

Commit complete.

SQL> create table dependent (ssnn char(9), dname varchar(15),


sex char(1) constr
aint dep_ch check(sex in ('M','F','m','f')),dob date, rel
varchar(8),constraint
dep_fkey foreign key(ssnn) references employee(ssnn) on delete
cascade);

Table created.

SQL> insert into dependent values('333445555',


'Alice','F','05-Apr-76','Daughter
');

1 row created.

SQL> insert into dependent values('333445555',


'Theodore','M','25-Oct-73','Son')
;

1 row created.

SQL> insert into dependent values('333445555',


'Joy','F','03-May-48','Spouse');

1 row created.

SQL> insert into dependent values('987654321',


Page 12
sample_constraint
'Abner','M','29-Feb-32','Spouse')
;

1 row created.

SQL> insert into dependent values('123456789',


'Alice','F','31-Dec-78','Daughter
');

1 row created.

SQL> insert into dependent values('123456789',


'Elizabeth','F','05-May-57','Spou
se');

1 row created.

SQL> select * from dependent;

SSNN DNAME S DOB REL


--------- --------------- - --------- --------
333445555 Alice F 05-APR-76 Daughter
333445555 Theodore M 25-OCT-73 Son
333445555 Joy F 03-MAY-48 Spouse
987654321 Abner M 29-FEB-32 Spouse
123456789 Alice F 31-DEC-78 Daughter
123456789 Elizabeth F 05-MAY-57 Spouse

6 rows selected.

SQL> commit;

Commit complete.
Page 13
sample_constraint

SQL> insert into employee values('Robert', 'F', 'Scott',


'987987987','21-JUN-42'
, '2365 Newcastle Rd, Bellaire, TX', 'M', 58000, '888665555',1);
insert into employee values('Robert', 'F', 'Scott',
'987987987','21-JUN-42', '23
65 Newcastle Rd, Bellaire, TX', 'M', 58000, '888665555',1)
*
ERROR at line 1:
ORA-00001: unique constraint (14BCE0903.EMP_SSNN) violated

SQL> insert into employee values('Ramez', 'F', 'Scott', '','21-JUN-42',


'2365 Ne
wcastle Rd, Bellaire, TX', 'M', 58000, '888665555',1);
insert into employee values('Ramez', 'F', 'Scott', '','21-JUN-42', '2365
Newcast
le Rd, Bellaire, TX', 'M', 58000, '888665555',1)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into
("14BCE0903"."EMPLOYEE"."SSNN")

SQL> insert into works_on values('677678989',null,'40.0');


insert into works_on values('677678989',null,'40.0')
*
ERROR at line 1:
ORA-02291: integrity constraint (14BCE0903.SSNN_FKEY) violated -
parent key not
found

Page 14
sample_constraint
SQL> insert into dependent values('453453453',
'John','M','12-Dec-60','Spouse');

1 row created.

SQL> insert into dependent values('343453453',


'Varun','','12-Dec-60','Son');
insert into dependent values('343453453', 'Varun','','12-Dec-60','Son')
*
ERROR at line 1:
ORA-02291: integrity constraint (14BCE0903.DEP_FKEY) violated -
parent key not
found

SQL> delete from works_on where ssnn='333445555';

4 rows deleted.

SQL> update works_on set mgrssn='123456789' and


mgrstartdate='01-OCT-88' where d
no=5;
update works_on set mgrssn='123456789' and
mgrstartdate='01-OCT-88' where dno=5
*
ERROR at line 1:
ORA-00933: SQL command not properly ended

SQL> update works_on set mgrssn='123456789',


mgrstartdate='01-OCT-88' where dno=
5;
Page 15
sample_constraint
update works_on set mgrssn='123456789', mgrstartdate='01-OCT-88'
where dno=5
*
ERROR at line 1:
ORA-00904: "DNO": invalid identifier

SQL> update dept set mgrssn='123456789',


mgrstartdate='01-OCT-88' where dno=5;
update dept set mgrssn='123456789', mgrstartdate='01-OCT-88'
where dno=5
*
ERROR at line 1:
ORA-00904: "DNO": invalid identifier

SQL> desc dept


Name Null? Type
----------------------------------------- -------- ----------------------------

DNAME NOT NULL VARCHAR2(15)


DEPNO NOT NULL NUMBER
MGRSSN CHAR(9)
MGRDATE DATE

SQL> update dept set mgrssn='123456789',


mgrstartdate='01-OCT-88' where depno=5;

update dept set mgrssn='123456789', mgrstartdate='01-OCT-88'


where depno=5
*
ERROR at line 1:
ORA-00904: "MGRSTARTDATE": invalid identifier
Page 16
sample_constraint

SQL> update dept set mgrssn='123456789', mgrdate='01-OCT-88'


where depno=5;

1 row updated.

SQL> desc project;


Name Null? Type
----------------------------------------- -------- ----------------------------

PNAME NOT NULL VARCHAR2(15)


PNUMBER NOT NULL NUMBER(5)
PLOCATION VARCHAR2(15)
DEPNO NUMBER(5)

SQL> alter table project add constraint pname_u unique (pname);

Table altered.

SQL> alter table employee modify(sex char(1) constraint emp_sex


NOT NULL);
alter table employee modify(sex char(1) constraint emp_sex NOT
NULL)
*
ERROR at line 1:
ORA-02264: name already used by an existing constraint

SQL> alter table employee modify(sex char(1) constraint


emp_notnull NOT NULL);

Table altered.
Page 17
sample_constraint

SQL> commit;

Commit complete.

Page 18

You might also like