Sei sulla pagina 1di 28

CREATE AND INSERT

SQL> CREATE TABLE Movie


2 (M_Id number(5) PRIMARY KEY,
3 Name varchar2(30),
4 Genre varchar2(30),
5 Year number(4),
6 Length number(4,2),
7 URL varchar2(50));

Table created.
SQL> CREATE TABLE Celebrity
2 (C_Id number(5) PRIMARY KEY,
3 Name varchar2(30),
4 DOB timestamp,
5 Sex varchar2(10));

Table created.
SQL> CREATE TABLE Production_Corporation
2 (Name varchar2(50) PRIMARY KEY,
3 Website varchar2(50),
4 Street varchar2(30),
5 City varchar2(30),
6 State varchar2(30),
7 Zip_Code number(10));

Table created.
SQL> CREATE TABLE M_User
2 (EMail varchar2(50) PRIMARY KEY,
3 Name varchar2(30),
4 Password varchar2(30),
5 Age number(3),
6 Sex varchar2(10));

Table created.
SQL> CREATE TABLE Role
2 (Name varchar2(50),
3 M_ID number(5),
4 C_ID number(5),
5 Role_Description varchar2(100),
6 PRIMARY KEY(Name, M_ID, C_ID),
7 CONSTRAINT fk_mid_mo FOREIGN KEY (M_ID) REFERENCES Movie(M_Id),
8 CONSTRAINT fk_cid_ce FOREIGN KEY (C_ID) REFERENCES Celebrity(C_Id));

Table created.

SQL> CREATE TABLE Review


2 (S_Number number(5),
3 M_ID number(5),
4 M_User_Email varchar2(50),
5 Score number(5),
6 Title varchar2(50),
7 Content varchar2(200),
8 Time_stamp timestamp,
9 PRIMARY KEY(S_Number, M_ID, M_User_Email),
10 CONSTRAINT fk_mid_mo_re FOREIGN KEY (M_ID) REFERENCES Movie(M_Id),
11 CONSTRAINT fk_mue_mu_re FOREIGN KEY (M_User_Email) REFERENCES M_User(EMail));

Table created.
SQL> CREATE TABLE Starring
2 (M_ID number(5),
3 C_ID number(5),
4 PRIMARY KEY(M_ID, C_ID),
5 CONSTRAINT fk_mid_mo_st FOREIGN KEY (M_ID) REFERENCES Movie(M_Id),
6 CONSTRAINT fk_cid_ce_st FOREIGN KEY (C_ID) REFERENCES Celebrity(C_Id));

Table created.
SQL> CREATE TABLE Produced_By
2 (M_ID number(5),
3 Production_Corporation_Name varchar2(50),
4 PRIMARY KEY(M_ID, Production_Corporation_Name),
5 CONSTRAINT fk_mid_mo_pb FOREIGN KEY (M_ID) REFERENCES Movie(M_Id),
6 CONSTRAINT fk_pcn_pc_pb FOREIGN KEY (Production_Corporation_Name) REFERENCES
Production_Corporation(Name));

Table created.

SQL> CREATE TABLE Watched_By


2 (M_User_Email varchar2(50),
3 M_ID number(5),
4 PRIMARY KEY(M_User_Email, M_ID),
5 CONSTRAINT fk_mue_mu_wb FOREIGN KEY (M_User_Email) REFERENCES M_User(EMail),
6 CONSTRAINT fk_mid_mo_wb FOREIGN KEY (M_ID) REFERENCES Movie(M_Id));

Table created.

SQL> desc movie;


Name Null? Type
----------------------------------------- -------- ----------------------------
M_ID NOT NULL NUMBER(5)
NAME VARCHAR2(30)
GENRE VARCHAR2(30)
YEAR NUMBER(4)
LENGTH NUMBER(4,2)
URL VARCHAR2(50)

SQL> desc celebrity;


Name Null? Type
----------------------------------------- -------- ----------------------------
C_ID NOT NULL NUMBER(5)
NAME VARCHAR2(30)
DOB TIMESTAMP(6)
SEX VARCHAR2(10)

SQL> desc production_corporation;


Name Null? Type
----------------------------------------- -------- ----------------------------
NAME NOT NULL VARCHAR2(50)
WEBSITE VARCHAR2(50)
STREET VARCHAR2(30)
CITY VARCHAR2(30)
STATE VARCHAR2(30)
ZIP_CODE NUMBER(10)

SQL> desc M_user;


Name Null? Type
----------------------------------------- -------- ----------------------------
EMAIL NOT NULL VARCHAR2(50)
NAME VARCHAR2(30)
PASSWORD VARCHAR2(30)
AGE NUMBER(3)
SEX VARCHAR2(10)

SQL> desc role;


Name Null? Type
----------------------------------------- -------- ----------------------------
NAME NOT NULL VARCHAR2(50)
M_ID NOT NULL NUMBER(5)
C_ID NOT NULL NUMBER(5)
ROLE_DESCRIPTION VARCHAR2(100)

SQL> desc review;


Name Null? Type
----------------------------------------- -------- ----------------------------
S_NUMBER NOT NULL NUMBER(5)
M_ID NOT NULL NUMBER(5)
M_USER_EMAIL NOT NULL VARCHAR2(50)
SCORE NUMBER(5)
TITLE VARCHAR2(50)
CONTENT VARCHAR2(200)
TIME_STAMP TIMESTAMP(6)
SQL> desc starring;
Name Null? Type
----------------------------------------- -------- ----------------------------
M_ID NOT NULL NUMBER(5)
C_ID NOT NULL NUMBER(5)

SQL> desc produced_by;


Name Null? Type
----------------------------------------- -------- ----------------------------
M_ID NOT NULL NUMBER(5)
PRODUCTION_CORPORATION_NAME NOT NULL VARCHAR2(50)

SQL> desc watched_by;


Name Null? Type
----------------------------------------- -------- ----------------------------
M_USER_EMAIL NOT NULL VARCHAR2(50)
M_ID NOT NULL NUMBER(5)

INSERTING DATA
SQL> INSERT INTO Movie VALUES
2 (&M_ID, '&NAME', '&GENRE', &YEAR, &LENGTH, '&URL');
Enter value for m_id: 1
Enter value for name: Black Panther
Enter value for genre: Adventure
Enter value for year: 2018
Enter value for length: 02.14
Enter value for url: https://www.imdb.com/title/tt1825683/
old 2: (&M_ID, '&NAME', '&GENRE', &YEAR, &LENGTH, '&URL')
new 2: (1, 'Black Panther', 'Adventure', 2018, 02.14,
'https://www.imdb.com/title/tt1825683/')

1 row created.
SQL> INSERT INTO Celebrity VALUES
2 (&C_ID, '&NAME', to_date('&DOB', 'DD-MM-YYYY'), '&SEX');
Enter value for c_id: 1
Enter value for name: Tim Robbins
Enter value for dob: 16-10-1958
Enter value for sex: Male
old 2: (&C_ID, '&NAME', to_date('&DOB', 'DD-MM-YYYY'), '&SEX')
new 2: (1, 'Tim Robbins', to_date('16-10-1958', 'DD-MM-YYYY'), 'Male')

1 row created.

SQL> INSERT INTO Production_Corporation VALUES


2 ('&NAME','&WEBSITE','&STREET','&CITY','&STATE', &ZIP_CODE);
Enter value for name: Marvel Studios
Enter value for website: www.marvel.com
Enter value for street: Buena Vista
Enter value for city: Burbank
Enter value for state: California
Enter value for zip_code: 91510
old 2: ('&NAME','&WEBSITE','&STREET','&CITY','&STATE', &ZIP_CODE)
new 2: ('Marvel Studios','www.marvel.com','Buena Vista','Burbank','California',
91510)

1 row created.

SQL> INSERT INTO M_User VALUES


2 ('&EMAIL','&NAME','&PASSWORD',&AGE,'&SEX');
Enter value for email: ashwani@email.com
Enter value for name: Ashwani Singh
Enter value for password: qwerty
Enter value for age: 19
Enter value for sex: Male
old 2: ('&EMAIL','&NAME','&PASSWORD',&AGE,'&SEX')
new 2: ('ashwani@email.com','Ashwani Singh','qwerty',19,'Male')

1 row created.
SQL> INSERT INTO Role VALUES
2 ('&NAME','&M_ID','&C_ID','&ROLE_DESCRIPTION');
Enter value for name: Andy Dufresne
Enter value for m_id: 5
Enter value for c_id: 1
Enter value for role_description: A banker, convicted of murdering his wife and
her lover.
old 2: ('&NAME','&M_ID','&C_ID','&ROLE_DESCRIPTION')
new 2: ('Andy Dufresne','5','1','A banker, convicted of murdering his wife and
her lover.')

1 row created.

SQL> INSERT INTO Review VALUES


2 (&S_NUMBER, &M_ID, '&M_USER_EMAIL', &SCORE, '&TITLE', '&CONTENT',
to_date('&TIMP_STAMP', 'DD-MM-YYYY HH24:MI:SS'));
Enter value for s_number: 1
Enter value for m_id: 1
Enter value for m_user_email: ashwani@email.com
Enter value for score: 7
Enter value for title: Overrated
Enter value for content: Not worth watching.
Enter value for timp_stamp: 02-09-2018 06:14:20
old 2: (&S_NUMBER, &M_ID, '&M_USER_EMAIL', &SCORE, '&TITLE', '&CONTENT',
to_date('&TIMP_STAMP', 'DD-MM-YYYY HH24:MI:SS'))
new 2: (1, 1, 'ashwani@email.com', 7, 'Overrated', 'Not worth watching.',
to_date('02-09-2018 06:14:20', 'DD-MM-YYYY HH24:MI:SS'))

1 row created.

SQL> INSERT INTO Produced_By VALUES


2 (&M_ID, '&PRODUCTION_CORPORATION_NAME');
Enter value for m_id: 1
Enter value for production_corporation_name: Marvel Studios
old 2: (&M_ID, '&PRODUCTION_CORPORATION_NAME')
new 2: (1, 'Marvel Studios')
1 row created.

SQL> INSERT INTO Watched_By VALUES


2 ('&M_USER_EMAIL', '&M_ID');
Enter value for m_user_email: ashwani@email.com
Enter value for m_id: 1
old 2: ('&M_USER_EMAIL', '&M_ID')
new 2: ('ashwani@email.com', '1')

1 row created.

Alter, Delete and update


ALTER
SQL> ALTER TABLE Review
2 MODIFY Title varchar2(40);

Table altered.

SQL> ALTER TABLE Produced_By


2 ADD Temp number(5);

Table altered.

SQL> ALTER TABLE Produced_By


2 MODIFY Temp varchar2(5);

Table altered.

DELETE
SQL> DELETE FROM Produced_By
2 WHERE M_ID = 1;
1 row deleted.
UPDATE
SQL> UPDATE Produced_By
2 SET Temp = 'Value'
3 WHERE M_ID = 1;

1 row updated.
SQL> UPDATE Produced_By
2 SET Temp = 'Sec'
3 WHERE Production_Corporation_Name = 'Universal Pictures';

1 row updated.
SQL> UPDATE Produced_By
2 SET Temp = 'Val'
3 WHERE M_ID > 3;

2 rows updated.

Primary key and foreign key constraint


Select with Where clause
SQL> SELECT M_ID, Name FROM Movie
2 WHERE Year > 2000;

M_ID NAME
---------- ------------------------------
1 Black Panther
3 First Man
4 3 Idiots

SQL> SELECT * FROM Watched_By


2 WHERE M_ID = 5;
M_USER_EMAIL M_ID
-------------------------------------------------- ----------
ashwani@email.com 5
nikhil@email.com 5
shivani@email.com 5
shubi@email.com 5

Any five comparison operators


SQL> SELECT Time_Stamp FROM Review
2 WHERE Score BETWEEN 9 AND 12;

TIME_STAMP
--------------------------------------------------------------------
-------
25-OCT-18 08.52.30.000000 AM
11-AUG-18 10.43.30.000000 PM
15-AUG-18 07.42.30.000000 PM
31-MAY-07 07.33.10.000000 AM

SQL> SELECT Password FROM M_User


2 WHERE Age > 20;

PASSWORD
------------------------------
bottle
elephant
SQL> SELECT Name FROM M_User
2 WHERE Name LIKE '%an%';

NAME
------------------------------
Ashwani Singh
Shivani Paul
Nikhil Chauhan
Surbhi Chauhan

SQL> SELECT Title FROM Review


2 WHERE S_Number = 1;

TITLE
----------------------------------------
Overrated
Brilliant
Great visuals.
Aal iz well.
Simply excellent.

SQL> SELECT Name FROM Production_Corporation


2 WHERE City != 'Mumbai';

NAME
--------------------------------------------------
Marvel Studios
Universal Pictures
Castle Rock Entertainment
Any five Aggregate functions
SQL> SELECT Avg(Score) FROM Review;

AVG(SCORE)
----------
8.75

SQL> SELECT Count(M_ID) FROM Movie;

COUNT(M_ID)
-----------
5

SQL> SELECT Max(Age) FROM M_User;

MAX(AGE)
----------
60

SQL> SELECT M_User_Email, Count(M_ID) FROM Watched_By


2 GROUP BY M_User_Email;

M_USER_EMAIL COUNT(M_ID)
-------------------------------------------------- -----------
ashwani@email.com 4
mohit@email.com 2
nikhil@email.com 3
shivani@email.com 4
shubi@email.com 5
SQL> SELECT Min(Count(M_ID)) FROM Watched_By
2 GROUP BY M_User_Email;

MIN(COUNT(M_ID))
----------------
2

Any five numeric functions


SQL> SELECT (Score - 8.5) AS DeviationScore
2 FROM Review;

DEVIATIONSCORE
--------------
-1.5
-4.5
-.5
4.5
-2.5
3.5
.5
2.5
-1.5
6.5
.5

DEVIATIONSCORE
--------------
-4.5

12 rows selected.
SQL> SELECT M_ID*C_ID AS Temp
2 FROM Role;

TEMP
----------
5
9
24
8
5
8

6 rows selected.

SQL> SELECT power(2,8) FROM Dual;

POWER(2,8)
----------
256s

SQL> SELECT Cos(60) FROM Dual;

COS(60)
----------
-.95241298

SQL> SELECT Ceil(-12.75) FROM Dual;

CEIL(-12.75)
------------
-12
Any five String Functions
SQL> SELECT UPPER(Website) FROM Production_Corporation;

UPPER(WEBSITE)
--------------------------------------------------
WWW.MARVEL.COM
WWW.SIPPY.NET
WWW.UNIVERSALPICTURES.COM
WWW.VINODCHOPRA.COM
WWW.CASTLEROCKENTERTAINMENT.COM

SQL> SELECT Replace(Website, 'www.', '.')


2 FROM Production_Corporation;

REPLACE(WEBSITE,'WWW.','.')
--------------------------------------------------
.marvel.com
.sippy.net
.universalpictures.com
.vinodchopra.com
.castlerockentertainment.com

SQL> SELECT Length(Website) FROM Production_Corporation;

LENGTH(WEBSITE)
---------------
14
13
25
19
31
SQL> SELECT Ascii(M_ID) FROM Movie;

ASCII(M_ID)
-----------
49
50
51
52
53

SQL> SELECT Concat(Name, Genre)


2 FROM Movie;

CONCAT(NAME,GENRE)
------------------------------------------------------------
Black PantherAdventure
SholayAction
First ManBiography
3 IdiotsDrama
Shawshank RedemptionDrama

Any five date functions


SQL> SELECT Sysdate FROM Dual;

SYSDATE
---------
27-OCT-18
SQL> SELECT Current_Timestamp FROM Dual;

CURRENT_TIMESTAMP
--------------------------------------------------------------------
-------
27-OCT-18 07.18.21.132000 PM +05:30

SQL> SELECT Add_Months(DOB,2) FROM Celebrity;

ADD_MONTH
---------
16-DEC-58
14-MAY-65
16-JUN-84
11-DEC-42
29-JAN-78
21-NOV-80

6 rows selected.

SQL> SELECT Months_Between


2 (To_Date('02-02-1995','DD-MM-YYYY'),
3 To_Date('01-01-1996','DD-MM-YYYY'))
4 AS Months FROM Dual;

MONTHS
----------
-10.967742
SQL> SELECT Last_Day(Sysdate) AS LastDay
2 FROM Dual;

LASTDAY
---------
31-OCT-18

Any three conversion functions


SQL> SELECT To_Char(DOB, 'MONTH DD, YYYY')
2 FROM Celebrity;

TO_CHAR(DOB,'MONTH DD, YYYY')


---------------------------------------------
OCTOBER 16, 1958
MARCH 14, 1965
APRIL 16, 1984
OCTOBER 11, 1942
NOVEMBER 29, 1977
SEPTEMBER 21, 1980

6 rows selected.

SQL> SELECT To_Date('01-01-1996','DD-MM-YYYY')


2 FROM Dual;

TO_DATE('
---------
01-JAN-96
SQL> SELECT To_Number('123') FROM Dual;

TO_NUMBER('123')
----------------
123

Three queries based on set operators


SQL> SELECT M_ID FROM Watched_By
2 WHERE M_User_Email = 'ashwani@email.com'
3 INTERSECT
4 SELECT M_ID FROM Watched_By
5 WHERE M_User_Email = 'nikhil@email.com';

M_ID
----------
1
2
5

SQL> SELECT Name FROM M_User


2 WHERE Sex = 'Female'
3 UNION
4 SELECT Name FROM Celebrity
5 WHERE Sex = 'Female';

NAME
------------------------------
Claire Foy
Kareena Kapoor
Shivani Paul
Surbhi Chuahan
SQL> SELECT Name FROM Role
2 WHERE M_ID = 4
3 MINUS
4 SELECT Name FROM Role
5 WHERE M_ID = 4 AND C_ID = 6;

NAME
--------------------------------------------------
Rancho

Group by and having


SQL> SELECT Min(Count(M_ID)) FROM Watched_By
2 GROUP BY M_User_Email;

MIN(COUNT(M_ID))
----------------
2

SQL> SELECT M_User_Email FROM Watched_By


2 GROUP BY M_User_Email
3 HAVING Count(M_ID) > 3;

M_USER_EMAIL
--------------------------------------------------
ashwani@email.com
shivani@email.com
shubi@email.com
Order by clause
SQL> SELECT Title FROM Review
2 Where M_ID = 4
3 ORDER BY Score;

TITLE
----------------------------------------
Good
Aal iz well.
Loved it

Join more than two tables (3 Queries)


SQL> SELECT C.Name, M.M_ID
2 FROM Celebrity C
3 INNER JOIN Role R on (C.C_ID = R.C_ID)
4 INNER JOIN Movie M on (R.M_ID = M.M_ID)
5 WHERE M.M_ID > 3;

NAME M_ID
------------------------------ ----------
Tim Robbins 5
Kareena Kapoor 4
Aamir Khan 4

SQL> SELECT Re.Content, R.Name


2 FROM Review Re
3 JOIN Role R ON (Re.M_ID = R.M_ID)
4 JOIN Movie M ON (R.M_ID = M.M_ID)
5 WHERE R.Name = 'Rancho';

CONTENT
--------------------------------------------------------------------
------------
NAME
--------------------------------------------------
It cured my depression.
Rancho

I liked it.
Rancho

Full of enjoyable drama.


Rancho

SQL> SELECT W.M_User_Email, P.State


2 FROM Watched_By W
3 JOIN Produced_By Pr ON (W.M_ID = Pr.M_ID)
4 JOIN Production_Corporation P
5 ON (Pr.Production_Corporation_Name = P.Name)
6 WHERE P.State = 'Maharashtra';

M_USER_EMAIL
--------------------------------------------------
STATE
------------------------------
ashwani@email.com
Maharashtra

mohit@email.com
Maharashtra

nikhil@email.com
Maharashtra
M_USER_EMAIL
--------------------------------------------------
STATE
------------------------------
shivani@email.com
Maharashtra

shubi@email.com
Maharashtra

shubi@email.com
Maharashtra

6 rows selected.

Sub Queries (3 Queries)


SQL> SELECT R.Name FROM Role R
2 WHERE R.C_ID IN
3 (SELECT C.C_ID FROM Celebrity C
4 WHERE C.Sex = 'Female');

NAME
--------------------------------------------------
Janet Shearon
Pia
SQL> SELECT P.Name,P.Zip_Code
2 FROM Production_Corporation P
3 WHERE P.Name IN
4 (SELECT Pr.Production_Corporation_Name
5 FROM Produced_By Pr
6 WHERE Pr.M_ID < 3);

NAME ZIP_CODE
-------------------------------------------------- ----------
Marvel Studios 91510
Sippy Entertainment 400007

SQL> SELECT M.Age, subquery1.Movies


2 FROM M_User M,
3 (SELECT M_User_Email, Count(M_ID) AS Movies
4 FROM Watched_By
5 GROUP BY M_User_Email) subquery1
6 WHERE subquery1.M_User_Email = M.Email;

AGE MOVIES
---------- ----------
15 5
19 4
60 3
19 4
35 2
Create 2 views (if possible)

SQL> CREATE VIEW View1 AS


2 SELECT M.Name, M.Year, M.Length
3 FROM Movie M;

View created.

SQL> SELECT * FROM View1;

NAME YEAR LENGTH


------------------------------ ---------- ----------
Black Panther 2018 2.14
Sholay 1975 2.42
First Man 2018 2.14
3 Idiots 2009 2.5
Shawshank Redemption 1994 2.22

SQL> CREATE VIEW View2 AS


2 SELECT M.Name, R.Score
3 FROM Movie M INNER JOIN Review R
4 ON M.M_ID = R.M_ID
5 WHERE M.M_ID = 3;

View created.

SQL> SELECT * FROM View2;

NAME SCORE
------------------------------ ----------
First Man 12
First Man 9
SQL> DECLARE
2 CURSOR cursor1 IS SELECT Genre, Name FROM Movie;
3 Genre_Movie Movie.Genre%type;
4 Name_Movie Movie.Name%type;
5 BEGIN
6 OPEN cursor1;
7 LOOP
8 FETCH cursor1 INTO Genre_Movie, Name_Movie;
9 EXIT WHEN cursor1%NOTFOUND;
10 IF (Genre_Movie = 'Drama') THEN
11 DBMS_OUTPUT.PUT_LINE('Movie Name: '||Name_Movie);
12 END IF;
13 END LOOP;
14 CLOSE cursor1;
15 END;
16 /
Movie Name: 3 Idiots
Movie Name: Shawshank Redemption

PL/SQL procedure successfully completed.

SQL> CREATE OR REPLACE FUNCTION totalReviews


2 RETURN number
3 AS
4 total NUMBER(2);
5 mid Review.M_ID%type;
6 BEGIN
7 mid := &mid;
8 total := 0;
9 SELECT count(Title) INTO total FROM Review
10 WHERE M_ID = mid;
11 RETURN total;
12 END totalReviews;
13 /
Enter value for mid: 2
old 7: mid := &mid;
new 7: mid := 2;

Function created.

SQL> DECLARE
2 answer NUMBER;
3 BEGIN
4 answer := totalReviews();
5 DBMS_OUTPUT.PUT_LINE('Total Reviews: '||answer);
6 END;
7 /
Total Reviews: 2

PL/SQL procedure successfully completed.

SQL> CREATE OR REPLACE TRIGGER display_score_changes


2 BEFORE UPDATE ON Review
3 FOR EACH ROW
4 WHEN (NEW.M_ID > 0)
5 DECLARE
6 score_diff NUMBER(2);
7 BEGIN
8 score_diff := :NEW.Score - :OLD.Score;
9 DBMS_OUTPUT.PUT_LINE('Old Score: '||:OLD.Score);
10 DBMS_OUTPUT.PUT_LINE('New Score: '||:NEW.Score);
11 DBMS_OUTPUT.PUT_LINE('Score Difference: '||score_diff);
12 END;
13 /
Trigger created.

SQL> UPDATE Review


2 SET Score = Score + 3
3 WHERE M_ID = 3;

Old Score: 10
New Score: 13
Score Difference: 3
Old Score: 3
New Score: 6
Score Difference: 3
2 rows updated.