0% found this document useful (0 votes)
92 views9 pages

UCS1412 - Database Lab Assignment - 1 NAME:Prakash R ROLL NO:185001108

The document describes the creation of tables for a database to store music data. It creates tables for musicians, studios, albums, songs, artists and a join table for songs sung by artists. It inserts sample data into the tables and validates the constraints by attempting invalid inserts that should violate primary key, foreign key, check and not null constraints.

Uploaded by

prakash
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)
92 views9 pages

UCS1412 - Database Lab Assignment - 1 NAME:Prakash R ROLL NO:185001108

The document describes the creation of tables for a database to store music data. It creates tables for musicians, studios, albums, songs, artists and a join table for songs sung by artists. It inserts sample data into the tables and validates the constraints by attempting invalid inserts that should violate primary key, foreign key, check and not null constraints.

Uploaded by

prakash
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/ 9

UCS1412 – Database Lab

Assignment – 1

NAME:Prakash R
ROLL NO:185001108

SQL> REM Dropping all the old tables;
SQL> drop table sungby;

Table dropped.

SQL> drop table artist;

Table dropped.

SQL> drop table song;

Table dropped.

SQL> drop table album;

Table dropped.

SQL> drop table studio;

Table dropped.

SQL> drop table musician;

Table dropped.

SQL> rem Creating Musician Table;
SQL> create table musician
  2  (m_id varchar2(3) constraint m_id_pk primary key, m_name varchar2(20),
  3  m_birthplace char(20));

Table created.

SQL> desc musician;
 Name                                            Null?    Type
 ----------------------------------------------- -------- --------------------------------
 M_ID                                            NOT NULL VARCHAR2(3)
 M_NAME                                                   VARCHAR2(20)
 M_BIRTHPLACE                                             CHAR(20)

SQL> rem Creating Studio Table;
SQL> create table studio
  2  ( stu_name varchar(20) constraint stu_name_pk primary key, stu_add varchar2(20),
  3  stu_ph number(10));

Table created.

SQL> desc studio;
 Name                                            Null?    Type
 ----------------------------------------------- -------- --------------------------------
 STU_NAME                                        NOT NULL VARCHAR2(20)
 STU_ADD                                                  VARCHAR2(20)
 STU_PH                                                   NUMBER(10)

SQL> rem Creating Album Table;
SQL> create table album
  2  (alb_name varchar2(20), alb_id varchar2(3) constraint alb_id_pk primary key,
  3  yr_of_rel number(4) constraint yr_of_rel_ch check (yr_of_rel >= 1945),
  4  no_of_tracks number(2) constraint ntracks_nn not null,
  5  stu_name varchar2(20) constraint stu_name_fk references studio(stu_name),
  6  genre varchar2(10) constraint gen_ch1 check (genre in('CAT', 'DIV' ,'MOV', 'POP')),
  7  m_id varchar2(3) constraint m_id_fk references musician(m_id));

Table created.

SQL> desc album;
 Name                                            Null?    Type
 ----------------------------------------------- -------- --------------------------------
 ALB_NAME                                                 VARCHAR2(20)
 ALB_ID                                          NOT NULL VARCHAR2(3)
 YR_OF_REL                                                NUMBER(4)
 NO_OF_TRACKS                                    NOT NULL NUMBER(2)
 STU_NAME                                                 VARCHAR2(20)
 GENRE                                                    VARCHAR2(10)
 M_ID                                                     VARCHAR2(3)

SQL> rem Creating Song Table
SQL> create table song
  2  (alb_id varchar2(3) constraint alb_id_fk references album(alb_id),
  3  track_no number(2), song_name varchar2(15), song_len number(2),
  4  genre varchar2(10) constraint gen_ch2 check (genre in ('PHI', 'REL', 'LOV', 'DEV', 'PAT')
),
  5  constraint song_pk primary key(alb_id, track_no),
  6  constraint song_len_ch check ((genre != 'PAT') or (song_len > 7)));

Table created.

SQL> desc song;
 Name                                            Null?    Type
 ----------------------------------------------- -------- --------------------------------
 ALB_ID                                          NOT NULL VARCHAR2(3)
 TRACK_NO                                        NOT NULL NUMBER(2)
 SONG_NAME                                                VARCHAR2(15)
 SONG_LEN                                                 NUMBER(2)
 GENRE                                                    VARCHAR2(10)

SQL> rem Creating Artist Table
SQL> create table artist
  2  (art_id varchar2(3) constraint art_id_pk primary key,
  3  art_name varchar2(20) constraint art_name_unq unique);

Table created.

SQL> desc artist;
 Name                                            Null?    Type
 ----------------------------------------------- -------- --------------------------------
 ART_ID                                          NOT NULL VARCHAR2(3)
 ART_NAME                                                 VARCHAR2(20)

SQL> rem Creating SungBy Table
SQL> create table sungby
  2  (alb_id varchar2(3), art_id varchar2(3), track_no number(2), rec_date date,
  3  constraint sungby_pk primary key(alb_id, art_id, track_no),
  4  constraint alb_track_fk foreign key (alb_id, track_no) references song(alb_id, track_no),
  5  constraint art_id_fk foreign key (art_id) references artist(art_id));

Table created.

SQL> desc sungby;
 Name                                            Null?    Type
 ----------------------------------------------- -------- --------------------------------
 ALB_ID                                          NOT NULL VARCHAR2(3)
 ART_ID                                          NOT NULL VARCHAR2(3)
 TRACK_NO                                        NOT NULL NUMBER(2)
 REC_DATE                                                 DATE

SQL> rem inserting 1st record;
SQL> insert into musician VALUES ('m01','amith', 'erode');

1 row created.

SQL> rem violating the primary key constraint;
SQL> insert into musician VALUES ('m01','kumar','erode');
insert into musician VALUES ('m01','kumar','erode')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.M_ID_PK) violated 

SQL> rem inserting the 2nd record;
SQL> insert into musician VALUES ('m02','anirudh','chennai');

1 row created.

SQL> SELECT * from musician;

M_I M_NAME               M_BIRTHPLACE                                                     
--- -------------------- --------------------                                             
m01 amith                erode                                                            
m02 anirudh              chennai                                                          

SQL> rem inserting 1st record;
SQL> insert into studio VALUES('saregama','t nagar, chennai',9876543210);

1 row created.

SQL> rem violating the studio name primary key constraint;
SQL> insert into studio VALUES('saregama','anna nagar, chennai',9988776655);
insert into studio VALUES('saregama','anna nagar, chennai',9988776655)
*
ERROR at line 1:
ORA-00001: unique constraint (HR.STU_NAME_PK) violated 

SQL> rem inserting the 2nd record;
SQL> insert into studio VALUES('think music','anna nagar, chennai',9988776655);

1 row created.

SQL> SELECT * from studio;

STU_NAME             STU_ADD                  STU_PH                                      
-------------------- -------------------- ----------                                      
saregama             t nagar, chennai     9876543210                                      
think music          anna nagar, chennai  9988776655                                      
SQL> rem inserting the 1st record;
SQL> insert into album VALUES('madras gig','a01',2018,2,'saregama','CAT','m01');

1 row created.

SQL> rem violating the album id primary key constraint;
SQL> insert into album VALUES('madras gig','a01',2018,2,'saregama','CAT','m01');
insert into album VALUES('madras gig','a01',2018,2,'saregama','CAT','m01')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.ALB_ID_PK) violated 

SQL> rem violating the year of release check constraint;
SQL> insert into album VALUES('madras gig','a01',1944,2,'saregama','CAT','m01')
  2  rem violating the no of tracks not null constraint;
rem violating the no of tracks not null constraint
*
ERROR at line 2:
ORA-00933: SQL command not properly ended 

SQL> insert into album VALUES('madras gig','a02',2018,NULL,'saregama','CAT','m01');
insert into album VALUES('madras gig','a02',2018,NULL,'saregama','CAT','m01')
                                                 *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("HR"."ALBUM"."NO_OF_TRACKS") 

SQL> rem violating the genre check constraint;
SQL> insert into album VALUES('madras gig','a02',2018,2,'saregamapa','CARNATIC','m01');
insert into album VALUES('madras gig','a02',2018,2,'saregamapa','CARNATIC','m01')
*
ERROR at line 1:
ORA-02290: check constraint (HR.GEN_CH1) violated 

SQL> rem violating the musician id foreign key constraint;
SQL> insert into album VALUES('madras gig','a02',2018,2,'saregama','CAT','m03');
insert into album VALUES('madras gig','a02',2018,2,'saregama','CAT','m03')
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.M_ID_FK) violated - parent key not found 

SQL> rem inserting 2nd record;
SQL> insert into album VALUES('shades of love','a02',2019,1,'think music','CAT','m02');

1 row created.

SQL> SELECT * from album;

ALB_NAME             ALB  YR_OF_REL NO_OF_TRACKS STU_NAME             GENRE      M_I      
-------------------- --- ---------- ------------ -------------------- ---------- ---      
madras gig           a01       2018            2 saregama             CAT        m01      
shades of love       a02       2019            1 think music          CAT        m02      

SQL> rem inserting 1st record;
SQL> insert into song VALUES('a01',1,'orasaadha',10,'PHI');

1 row created.

SQL> rem violating the song primary key constraint;
SQL> insert into song VALUES('a01',1,'orasaadha',10,'PHI');
insert into song VALUES('a01',1,'orasaadha',10,'PHI')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.SONG_PK) violated 

SQL> rem violating genre check constraint;
SQL> insert into song VALUES('a01',2,'kanne kanne',10,'LOVE');
insert into song VALUES('a01',2,'kanne kanne',10,'LOVE')
*
ERROR at line 1:
ORA-02290: check constraint (HR.GEN_CH2) violated 

SQL> rem violating the album id foreig key constraint;
SQL> insert into song VALUES('a03',2,'kanne kanne',10,'LOV');
insert into song VALUES('a03',2,'kanne kanne',10,'LOV')
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.ALB_ID_FK) violated - parent key not found 

SQL> rem inserting 2nd record;
SQL> insert into song VALUES('a01',2,'kanne kanne',10,'LOV');

1 row created.

SQL> rem violating song length check constraint;
SQL> insert into song VALUES('a02',1,'tamizhanda',5,'PAT')
  2  rem inserting 3rd record;
rem inserting 3rd record
*
ERROR at line 2:
ORA-00933: SQL command not properly ended 

SQL> insert into song VALUES('a02',1,'tamizhanda',8,'PAT');

1 row created.

SQL> SELECT * from song;

ALB   TRACK_NO SONG_NAME         SONG_LEN GENRE                                           
--- ---------- --------------- ---------- ----------                                      
a01          1 orasaadha               10 PHI                                             
a01          2 kanne kanne             10 LOV                                             
a02          1 tamizhanda               8 PAT                                             

SQL> rem inserting 1st record;
SQL> INSERT INTO artist VALUES('ar1','anirudh');

1 row created.

SQL> rem violating arting id primary key constraint;
SQL> INSERT INTO artist VALUES('ar1','aadhi');
INSERT INTO artist VALUES('ar1','aadhi')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.ART_ID_PK) violated 

SQL> rem inserting 2nd record;
SQL> INSERT INTO artist VALUES('ar2','anirudh');
INSERT INTO artist VALUES('ar2','anirudh')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.ART_NAME_UNQ) violated 
SQL> rem violating artist name unique constraint;
SQL> INSERT INTO artist VALUES('ar2','aadhi');

1 row created.

SQL> SELECT * from artist;

ART ART_NAME                                                                              
--- --------------------                                                                  
ar1 anirudh                                                                               
ar2 aadhi                                                                                 

SQL> rem inserting 1st record;
SQL> insert into sungby VALUES('a01','ar1',1,'03-jan-2019');

1 row created.

SQL> rem violating sungby primary key constraint;
SQL> insert into sungby VALUES('a01','ar1',1,'01-jan-2019');
insert into sungby VALUES('a01','ar1',1,'01-jan-2019')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.SUNGBY_PK) violated 

SQL> rem violating albumid trackid foreign key constraint;
SQL> insert into sungby VALUES('a03','ar1',2,'01-jan-2019');
insert into sungby VALUES('a03','ar1',2,'01-jan-2019')
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.ALB_TRACK_FK) violated - parent key not found 

SQL> rem violating albumid trackid foreign key constraint;
SQL> insert into sungby VALUES('a01','ar3',1,'01-jan-2019');
insert into sungby VALUES('a01','ar3',1,'01-jan-2019')
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.ART_ID_FK) violated - parent key not found 

SQL> rem inserting 2nd record;
SQL> insert into sungby VALUES('a01','ar1',2,'02-apr-2019');

1 row created.

SQL> rem inserting 3rd record;
SQL> insert into sungby values('a02','ar2',1,'13-jul-2018');

1 row created.

SQL> select * from sungby;

ALB ART   TRACK_NO REC_DATE                                                               
--- --- ---------- ---------                                                              
a01 ar1          1 03-JAN-19                                                              
a01 ar1          2 02-APR-19                                                              
a02 ar2          1 13-JUL-18                                                              

SQL> rem modifying the table according to the specified requirements;
SQL> alter table artist
  2  add art_gender varchar2(1);
Table altered.

SQL> update table artist
  2  set art_gender = 'M' where art_id = 'ar1';
update table artist
       *
ERROR at line 1:
ORA-00903: invalid table name 

SQL> update table artist
  2  set art_gender = 'M' where art_id = 'ar2';
update table artist
       *
ERROR at line 1:
ORA-00903: invalid table name 

SQL> alter table song
  2  modify song_len number(20);

Table altered.

SQL> alter table studio
  2  add constraint stu_ph_unq unique(stu_ph);

Table altered.

SQL> alter table sungby
  2  modify rec_date not null;

Table altered.

SQL> alter table song
  2  drop constraint gen_ch2;

Table altered.

SQL> alter table song
  2  add constraint gen_ch2 check (genre in ('PHI', 'REL', 'LOV', 'DEV', 'PAT', 'NAT'));

Table altered.

SQL> alter table sungby
  2  drop constraint alb_track_fk;

Table altered.

SQL> alter table sungby
  2  add constraint alb_track_fk foreign key (alb_id, track_no) references song(alb_id, track_
no) on delete cascade;

Table altered.

SQL> rem inserting 3rd record to artist table after alteration;
SQL> INSERT INTO artist VALUES('ar3','yuvan','M');

1 row created.

SQL> select * from artist;

ART ART_NAME             A                                                                
--- -------------------- -                                                                
ar1 anirudh                                                                               
ar2 aadhi                                                                                 
ar3 yuvan                M                                                                

SQL> rem violating phone number unique constraint;
SQL> insert into studio VALUES('lahari','tambaram, chennai',9876543210);
insert into studio VALUES('lahari','tambaram, chennai',9876543210)
*
ERROR at line 1:
ORA-00001: unique constraint (HR.STU_PH_UNQ) violated 

SQL> select * from studio;

STU_NAME             STU_ADD                  STU_PH                                      
-------------------- -------------------- ----------                                      
saregama             t nagar, chennai     9876543210                                      
think music          anna nagar, chennai  9988776655                                      

SQL> rem violating record date not null constraint;
SQL> insert into sungby VALUES('a01','ar1',1,NULL);
insert into sungby VALUES('a01','ar1',1,NULL)
                                        *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("HR"."SUNGBY"."REC_DATE") 

SQL> select * from sungby;

ALB ART   TRACK_NO REC_DATE                                                               
--- --- ---------- ---------                                                              
a01 ar1          1 03-JAN-19                                                              
a01 ar1          2 02-APR-19                                                              
a02 ar2          1 13-JUL-18                                                              

SQL> rem inserting new record with genre 'NAT';
SQL> insert into song VALUES('a02',2,'bhoomi bhoomi',5,'NAT');

1 row created.

SQL> rem before deletion;
SQL> SELECT * from song;

ALB   TRACK_NO SONG_NAME         SONG_LEN GENRE                                           
--- ---------- --------------- ---------- ----------                                      
a01          1 orasaadha               10 PHI                                             
a01          2 kanne kanne             10 LOV                                             
a02          1 tamizhanda               8 PAT                                             
a02          2 bhoomi bhoomi            5 NAT                                             

SQL> select * from sungby;

ALB ART   TRACK_NO REC_DATE                                                               
--- --- ---------- ---------                                                              
a01 ar1          1 03-JAN-19                                                              
a01 ar1          2 02-APR-19                                                              
a02 ar2          1 13-JUL-18                                                              

SQL> rem deleting album 1;
SQL> DELETE from song WHERE alb_id = 'a01';

2 rows deleted.
SQL> rem after deletion;
SQL> SELECT * from song;

ALB   TRACK_NO SONG_NAME         SONG_LEN GENRE                                           
--- ---------- --------------- ---------- ----------                                      
a02          1 tamizhanda               8 PAT                                             
a02          2 bhoomi bhoomi            5 NAT                                             

SQL> select * from sungby;

ALB ART   TRACK_NO REC_DATE                                                               
--- --- ---------- ---------                                                              
a02 ar2          1 13-JUL-18                                                              

SQL> SPOOL OFF

You might also like