0% found this document useful (0 votes)
168 views17 pages

Boat SQL Word

The document shows SQL commands used to create tables, add/modify constraints, and insert data into an Oracle database. Tables are created for boats, sailors, tourists and reservations. Constraints are added to check types, ages, dates etc. Data is inserted and queried from the tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views17 pages

Boat SQL Word

The document shows SQL commands used to create tables, add/modify constraints, and insert data into an Oracle database. Tables are created for boats, sailors, tourists and reservations. Constraints are added to check types, ages, dates etc. Data is inserted and queried from the tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

SQL*Plus: Release 10.2.0.1.

0 - Production on Mon Jan 27 18:52:02 2014

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options

SQL> REM>DISCARDING ALL TABLES SQL> drop table reservation; drop table reservation * ERROR at line 1: ORA-00942: table or view does not exist

SQL> drop table sailor; drop table sailor * ERROR at line 1: ORA-00942: table or view does not exist

SQL> drop table tourist; drop table tourist * ERROR at line 1: ORA-00942: table or view does not exist

SQL> drop table boat; drop table boat * ERROR at line 1: ORA-00942: table or view does not exist

SQL> drop table boat; drop table boat * ERROR at line 1: ORA-00942: table or view does not exist

SQL> REM> CREATING TABLE BOAT SQL> commit;

Commit complete.

SQL> create table boat(id number(5) primary key,name varchar(10),type varchar(10),capacity number(10),price number(10),colour varchar(10));

Table created.

SQL> REM>QUERY 1:ADDING CONSTRAINT FOR BOAT TYPE SQL> alter table boat add constraint boat_type check(type='lux' or type='car' or type='cru');

Table altered.

SQL> REM> ENTERING THE CONTENTS OF BOAT TABLE

SQL> insert into boat values(1001,'Hydro','lux',100,100,'pink');

1 row created.

SQL> insert into boat values(1002,'Thunder','cru',75,75,'yellow');

1 row created.

SQL> insert into boat values(1003,'Stromer','car',50,50,'green');

1 row created.

SQL> REM> DISPLAYING TABLE BOAT SQL> select * from boat;

ID NAME

TYPE

CAPACITY

PRICE COLOUR

---------- ---------- ---------- ---------- ---------- ---------1001 Hydro lux 100 75 50 100 pink 75 yellow 50 green

1002 Thunder cru 1003 Stromer car

SQL> REM> CHECKING BOAT TYPE CONSTRAINT SQL> insert into boat values(1004,'Wiper','yacht',80,80,'orange'); insert into boat values(1004,'Wiper','yacht',80,80,'orange') * ERROR at line 1: ORA-02290: check constraint (SCOTT.BOAT_TYPE) violated

SQL> REM> CREATING TABLE SAILOR SQL> create table sailor(id number(5) primary key,name varchar(10),rating varchar(10),dob date);

Table created.

SQL> REM>QUERY 2: ADDING CONSTRAINT RATING FOR A SAILOR SQL> alter table sailor add constraint rating_type check(rating='a' or rating='b' or rating='c');

Table altered.

SQL> REM> INSERTING VALUES TO SAILOR TABLE SQL> insert into sailor values(2001,'lokkesh','a','30-AUG-1993');

1 row created.

SQL> insert into sailor values(2002,'ram','c','11-FEB-1990');

1 row created.

SQL> insert into sailor values(2003,'sujitha','b','13-JAN-1995');

1 row created.

SQL> REM> DISPLAYING SAILOR TABLE SQL> select * from sailor;

ID NAME

RATING

DOB

---------- ---------- ---------- --------2001 lokkesh a 2002 ram c 30-AUG-93 11-FEB-90 13-JAN-95

2003 sujitha b

SQL> REM> CHECKING SAILOR RATING CONSTRAINT

SQL> insert into sailor values(2004,'siva','d','10-NOV-1980'); insert into sailor values(2004,'siva','d','10-NOV-1980') * ERROR at line 1: ORA-02290: check constraint (SCOTT.RATING_TYPE) violated

SQL> REM> CREATING TABLE TOURIST SQL> commit;

Commit complete.

SQL> create table tourist(tno number(10) primary key,address varchar(10),dob date,phone number(10));

Table created.

SQL> REM> ENTERING VALUES TO TOURISTS TABLE SQL> insert into tourist values(3001,'chennai','30-AUG-1995',9884031447);

1 row created.

SQL> drop table tourist;

Table dropped.

SQL> create table tourist(tno number(10) primary key,name varchar(10),address varchar(10),dob date,phone number(10));

Table created.

SQL> insert into tourist values (3001,'ramesh','30-AUG-1995',9444243166);

insert into tourist values (3001,'ramesh','30-AUG-1995',9444243166) * ERROR at line 1: ORA-00947: not enough values

SQL> REM> ERROR ABOVE SQL> insert into tourist values (3001,'ramesh','chennai','30-AUG-1995',9884031447);

1 row created.

SQL> insert into tourist values ( 3002,'praveen','kachipuram','12-NOV-1980',8939155347);

1 row created.

SQL> insert into tourist values (3003,'suganya','madurai','30-JAN-2000',99401701826); insert into tourist values (3003,'suganya','madurai','30-JAN-2000',99401701826) * ERROR at line 1: ORA-01438: value larger than specified precision allowed for this column

SQL> insert into tourist values (3003,'suganya','madurai','30-JAN-2000',9940170182);

1 row created.

SQL> REM> DISPLAYING TOURIST TABLE SQL> select * from tourist;

TNO NAME

ADDRESS DOB

PHONE

---------- ---------- ---------- --------- ----------

3001 ramesh

chennai 30-AUG-95 9884031447

3002 praveen kachipuram 12-NOV-80 8939155347 3003 suganya madurai 30-JAN-00 9940170182

SQL> commit;

Commit complete.

SQL> REM> CREATING TABLE RESERVATION SQL> REM>QUERY 6,7,15 SQL> create table reservation(bno number(10),sno number(10),tno number(10),primary key(bno,sno,tno),dos date,dor date);

Table created.

SQL> alter table reservation add constraint res_fk1 foreign key (bno) references boat(id) on delete cascade;

Table altered.

SQL> alter table reservation add constraint res_fk2 foreign key (sno) references sailor(id) on delete cascade;

Table altered.

SQL> alter table reservation add constraint res_fk3 foreign key (tno) references tourist(tno) on delete cascade;

Table altered.

SQL> REM>QUERY 3 SQL> alter table reservation add constraint dos_check check(dos-dor<=12);

Table altered.

SQL> insert into reservation values(1001,2001,3001,'31-JAN-2014','28-JAN-2014');

1 row created.

SQL> select * from reservation;

BNO

SNO

TNO DOS

DOR

---------- ---------- ---------- --------- --------1001 2001 3001 31-JAN-14 28-JAN-14

SQL> REM> VIOLATING QUERY 3 SQL> insert into reservation values(1001,2001,3001,'31-JAN-2014','08-JAN-2014'); insert into reservation values(1001,2001,3001,'31-JAN-2014','08-JAN-2014') * ERROR at line 1: ORA-02290: check constraint (SCOTT.DOS_CHECK) violated

SQL> REM> QUERY 4: SQL> alter table sailor add constraint age_check check(((months_between('20-JAN2014',dob))/12>25)); alter table sailor add constraint age_check check(((months_between('20-JAN-2014',dob))/12>25)) * ERROR at line 1: ORA-02293: cannot validate (SCOTT.AGE_CHECK) - check constraint violated

SQL> alter table sailor add constraint age_check check(((months_between('20-JAN2014',dob))/12>25));

alter table sailor add constraint age_check check(((months_between('20-JAN-2014',dob))/12>25)) * ERROR at line 1: ORA-02293: cannot validate (SCOTT.AGE_CHECK) - check constraint violated

SQL> REM ERROR ABOVE DELETE IT SQL> delete from sailor;

3 rows deleted.

SQL> REM> INSERTING INTO SAILOR TABLE SQL> alter table sailor add constraint age_check check(((months_between('20-JAN2014',dob))/12>25));

Table altered.

SQL> insert into sailor values(2001,'gautam','a','29-OCT-1983');

1 row created.

SQL> insert into sailor values (2002,'sujitha','b','23-JAN-1972');

1 row created.

SQL> insert into sailor values (2003,'ram','c','18-FEB-1889');

1 row created.

SQL> REM> VOILATION OF QUERY 4 SQL> select * from sailor;

ID NAME

RATING

DOB

---------- ---------- ---------- --------2001 gautam 2002 sujitha b 2003 ram c a 29-OCT-83 23-JAN-72 18-FEB-89

SQL> insert into sailor values(2004,'kamal','c','22-OCT-2000'); insert into sailor values(2004,'kamal','c','22-OCT-2000') * ERROR at line 1: ORA-02290: check constraint (SCOTT.AGE_CHECK) violated

SQL> REM> QUERY 8 SQL> alter table tourist add no_child number(10);

Table altered.

SQL> insert into tourist values(3004,'rajiv','20-JUL-1995',8939303456,2); insert into tourist values(3004,'rajiv','20-JUL-1995',8939303456,2) * ERROR at line 1: ORA-00947: not enough values

SQL> insert into tourist values(3004,'rajiv','banglore','20-JUL-1995',8939303456,2);

1 row created.

SQL> select * from tourist;

TNO NAME

ADDRESS DOB

PHONE NO_CHILD

---------- ---------- ---------- --------- ---------- ---------3001 ramesh chennai 30-AUG-95 9884031447

3002 praveen kachipuram 12-NOV-80 8939155347 3003 suganya madurai 30-JAN-00 9940170182 3004 rajiv banglore 20-JUL-95 8939303456 2

SQL> REM> QUERY 8 OVER SQL> commit;

Commit complete.

SQL> REM>VIOLATION OF QUERY 9 SQL> insert into tourist values(3005,'vinod sharma','chennai','13-MAR-1994',9445623466,3); insert into tourist values(3005,'vinod sharma','chennai','13-MAR-1994',9445623466,3) * ERROR at line 1: ORA-12899: value too large for column "SCOTT"."TOURIST"."NAME" (actual: 12, maximum: 10)

SQL> REM>QUERY 9 SQL> alter table tourist modify name varchar(20);

Table altered.

SQL> REM>SUCCESS OF QUERY 9 SQL> insert into tourist values(3005,'vinod sharma','chennai','13-MAR-1994',9445623466,3);

1 row created.

SQL> REM > DISPLAY TOURIST TABLE AFTER ALTERATION & INSERTION SQL> select * from tourist;

TNO NAME

ADDRESS DOB

PHONE NO_CHILD

---------- -------------------- ---------- --------- ---------- ---------3001 ramesh 3002 praveen 3003 suganya 3004 rajiv chennai 30-AUG-95 9884031447 kachipuram 12-NOV-80 8939155347 madurai 30-JAN-00 9940170182 banglore 20-JUL-95 8939303456 2 3

3005 vinod sharma

chennai 13-MAR-94 9445623466

SQL> commit;

Commit complete.

SQL> REM>QUERY 10 SQL> alter table reservation modify dor not null;

Table altered.

SQL> insert into reservation values(1001,2001,3001,,'23-DEC-2003'); insert into reservation values(1001,2001,3001,,'23-DEC-2003') * ERROR at line 1: ORA-00936: missing expression

SQL> insert into reservation values(1001,2001,3001,null,'23-DEC-2003');

1 row created.

SQL> select * from reservation;

BNO

SNO

TNO DOS

DOR

---------- ---------- ---------- --------- --------1001 2001 3001 23-DEC-03

SQL> REM>QUERY 13 SQL> REM> QUERY 12 SQL> alter table sailor drop constraint rating_type;

Table altered.

SQL> alter table sailor add constraint rating_type check(rating='a' or rating='b' or rating='c' or rating='d');

Table altered.

SQL> REM>SUCCESS OF QUERY 12 SQL> insert into sailor values(2006,'friend','d','19-MAR-1980');

1 row created.

SQL> select * from sailor;

ID NAME

RATING

DOB

---------- ---------- ---------- --------2001 gautam 2002 sujitha b 2003 ram 2006 friend c d a 29-OCT-83 23-JAN-72 18-FEB-89 19-MAR-80

SQL> REM>QUERY 13 before SQL> select * from boat;

ID NAME

TYPE

CAPACITY

PRICE COLOUR

---------- ---------- ---------- ---------- ---------- ---------1001 Hydro lux 100 75 50 100 pink 75 yellow 50 green

1002 Thunder cru 1003 Stromer car

SQL> update boat set colour='yellow' where type='lux';

1 row updated.

SQL> select * from boat;

ID NAME

TYPE

CAPACITY

PRICE COLOUR

---------- ---------- ---------- ---------- ---------- ---------1001 Hydro lux 100 75 50 100 yellow 75 yellow 50 green

1002 Thunder cru 1003 Stromer car

SQL> REM> VIOLATION OF QUERY 13 SQL> alter table boat add constraint boat_colour check(type='lux' and colour='yellow'); alter table boat add constraint boat_colour check(type='lux' and colour='yellow') * ERROR at line 1: ORA-02293: cannot validate (SCOTT.BOAT_COLOUR) - check constraint violated

SQL> REM>QUERY 14

SQL> alter table boat add constraint boat_u unique(name);

Table altered.

SQL> select * from boat;

ID NAME

TYPE

CAPACITY

PRICE COLOUR

---------- ---------- ---------- ---------- ---------- ---------1001 Hydro lux 100 75 50 100 yellow 75 yellow 50 green

1002 Thunder cru 1003 Stromer car

SQL> REM > QUERY 14 VIOLATION SQL> insert into boat values(1004,'Hydro','car',50,50,'red'); insert into boat values(1004,'Hydro','car',50,50,'red') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.BOAT_U) violated

SQL> commit;

Commit complete.

SQL> REM ... SQL> desc tourist Name Null? Type

----------------------------------------- -------- ---------------------------TNO NAME ADDRESS NOT NULL NUMBER(10) VARCHAR2(20) VARCHAR2(10)

DOB PHONE NO_CHILD

DATE NUMBER(10) NUMBER(10)

SQL> select * from tourist;

TNO NAME

ADDRESS DOB

PHONE NO_CHILD

---------- -------------------- ---------- --------- ---------- ---------3001 ramesh 3002 praveen 3003 suganya 3004 rajiv chennai 30-AUG-95 9884031447 kachipuram 12-NOV-80 8939155347 madurai 30-JAN-00 9940170182 banglore 20-JUL-95 8939303456 2 3

3005 vinod sharma

chennai 13-MAR-94 9445623466

SQL> select * from reservation;

BNO

SNO

TNO DOS

DOR

---------- ---------- ---------- --------- --------1001 2001 3001 23-DEC-03

SQL> insert into reservation values(1001,2001,3001,'24-jan-2013','25-jan-2013'); insert into reservation values(1001,2001,3001,'24-jan-2013','25-jan-2013') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C005471) violated

SQL> select * from boat;

ID NAME

TYPE

CAPACITY

PRICE COLOUR

---------- ---------- ---------- ---------- ---------- ----------

1001 Hydro

lux

100 75 50

100 yellow 75 yellow 50 green

1002 Thunder cru 1003 Stromer car

SQL> drop table boat; drop table boat * ERROR at line 1: ORA-02449: unique/primary keys in table referenced by foreign keys

SQL> REM> QUERY 15 Deleting from boat SQL> delete from boat where id=1001;

1 row deleted.

SQL> select * from reservation;

no rows selected

SQL> spool off;

You might also like