def printit():
    print("""
    
    
create database lab1;
use lab1;

CREATE TABLE PUBLISHER
(NAME VARCHAR(20) PRIMARY KEY,
PHONE INTEGER,
ADDRESS VARCHAR(20));

CREATE TABLE BOOK
(BOOK_ID INTEGER PRIMARY KEY,
TITLE VARCHAR(20),
PUB_YEAR VARCHAR(20),
PUBLISHER_NAME varchar(20),
foreign key(PUBLISHER_NAME) REFERENCES PUBLISHER (NAME) ON DELETE CASCADE);

CREATE TABLE BOOK_AUTHORS
(AUTHOR_NAME VARCHAR(20),
BOOK_ID INTEGER,
foreign key(BOOK_ID) REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
PRIMARY KEY (BOOK_ID, AUTHOR_NAME));

CREATE TABLE LIBRARY_BRANCH
(BRANCH_ID INTEGER PRIMARY KEY,
BRANCH_NAME VARCHAR(50),
ADDRESS VARCHAR(50));

CREATE TABLE BOOK_COPIES
(NO_OF_COPIES INTEGER,
BOOK_ID INTEGER,
BRANCH_ID INTEGER,
foreign key(BOOK_ID) REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
foreign key(BRANCH_ID) REFERENCES LIBRARY_BRANCH (BRANCH_ID) ON DELETE 
CASCADE,
PRIMARY KEY (BOOK_ID, BRANCH_ID));

CREATE TABLE CARD
(CARD_NO INTEGER PRIMARY KEY);

CREATE TABLE BOOK_LENDING
(DATE_OUT DATE,
DUE_DATE DATE,
BOOK_ID INTEGER,
BRANCH_ID INTEGER,
CARD_NO INTEGER,
foreign key(BOOK_ID) REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
foreign key(BRANCH_ID) REFERENCES LIBRARY_BRANCH (BRANCH_ID) ON DELETE 
CASCADE,
foreign key(CARD_NO) REFERENCES CARD (CARD_NO) ON DELETE CASCADE,
PRIMARY KEY (BOOK_ID, BRANCH_ID, CARD_NO));

ALTER TABLE PUBLISHER    
MODIFY PHONE long;

INSERT INTO PUBLISHER VALUES ('MCGRAW-HILL', 9989076587, 'BANGALORE');
INSERT INTO PUBLISHER VALUES ('PEARSON', 9889076565, 'NEWDELHI');
INSERT INTO PUBLISHER VALUES ('RANDOM HOUSE', 7455679345, 'HYDRABAD');
INSERT INTO PUBLISHER VALUES ('HACHETTE LIVRE', 8970862340, 'CHENAI');
INSERT INTO PUBLISHER VALUES ('GRUPO PLANETA', 7756120238, 'BANGALORE');

INSERT INTO BOOK VALUES (1,'DBMS', 'JAN-2017', 'MCGRAW-HILL');
INSERT INTO BOOK VALUES (2,'ADBMS', 'JUN-2016', 'MCGRAW-HILL');
INSERT INTO BOOK VALUES (3,'CN', 'SEP-2016', 'PEARSON');
INSERT INTO BOOK VALUES (4,'CG', 'SEP-2015', 'GRUPO PLANETA');
INSERT INTO BOOK VALUES (5,'OS', 'MAY-2016', 'PEARSON');

INSERT INTO BOOK_AUTHORS VALUES ('NAVATHE', 1);
INSERT INTO BOOK_AUTHORS VALUES ('NAVATHE', 2);
INSERT INTO BOOK_AUTHORS VALUES ('TANENBAUM', 3);
INSERT INTO BOOK_AUTHORS VALUES ('EDWARD ANGEL', 4);
INSERT INTO BOOK_AUTHORS VALUES ('GALVIN', 5);

INSERT INTO LIBRARY_BRANCH VALUES (10,'RR NAGAR','BANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES (11,'RNSIT','BANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES (12,'RAJAJI NAGAR', 'BANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES (13,'NITTE','MANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES (14,'MANIPAL','UDUPI');

INSERT INTO BOOK_COPIES VALUES (10, 1, 10);
INSERT INTO BOOK_COPIES VALUES (5, 1, 11);
INSERT INTO BOOK_COPIES VALUES (2, 2, 12);
INSERT INTO BOOK_COPIES VALUES (5, 2, 13);
INSERT INTO BOOK_COPIES VALUES (7, 3, 14);
INSERT INTO BOOK_COPIES VALUES (1, 5, 10);
INSERT INTO BOOK_COPIES VALUES (3, 4, 11);


INSERT INTO CARD VALUES (100);
INSERT INTO CARD VALUES (101);
INSERT INTO CARD VALUES (102);
INSERT INTO CARD VALUES (103);
INSERT INTO CARD VALUES (104);


INSERT INTO BOOK_LENDING VALUES ('2017-01-01','2017-06-01', 1, 10, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-01-11','2017-03-11', 3, 14, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-02-21','2017-04-21', 2, 13, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-03-15','2017-07-15', 4, 11, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-04-12','2017-05-12', 1, 11, 104);


SELECT B.BOOK_ID, B.TITLE, B.PUBLISHER_NAME, A.AUTHOR_NAME, 
C.NO_OF_COPIES, L.BRANCH_ID
FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES C, LIBRARY_BRANCH L
WHERE B.BOOK_ID=A.BOOK_ID
AND B.BOOK_ID=C.BOOK_ID
AND L.BRANCH_ID=C.BRANCH_ID;


SELECT CARD_NO 
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN '2017-01-01' AND '2017-05-01'
GROUP BY CARD_NO
HAVING COUNT(*)>3;


DELETE FROM BOOK
WHERE BOOK_ID=3;
select * from BOOK;


CREATE VIEW V_PUBLICATION AS
SELECT PUB_YEAR
FROM BOOK;

select * from V_PUBLICATION;


CREATE VIEW V_BOOKS AS
SELECT B.BOOK_ID, B.TITLE, C.NO_OF_COPIES
FROM BOOK B, BOOK_COPIES C, LIBRARY_BRANCH L
WHERE B.BOOK_ID=C.BOOK_ID
AND C.BRANCH_ID=L.BRANCH_ID;

select * from V_BOOKS;



create database lab2;
use lab2;
CREATE TABLE SALESMAN
(SALESMAN_ID INT,
NAME VARCHAR (20),
CITY VARCHAR (20),
COMMISSION VARCHAR (20),
PRIMARY KEY (SALESMAN_ID));

CREATE TABLE CUSTOMER1
(CUSTOMER_ID INT,
CUST_NAME VARCHAR (20),
CITY VARCHAR (20),
GRADE INT,
SALESMAN_ID INT,
PRIMARY KEY (CUSTOMER_ID),
foreign key(SALESMAN_ID) REFERENCES SALESMAN (SALESMAN_ID) ON DELETE SET NULL);

CREATE TABLE ORDERS
(ORD_NO INT,
PURCHASE_AMT INT,
ORD_DATE DATE,
PRIMARY KEY (ORD_NO),
CUSTOMER_ID INT,
SALESMAN_ID INT,
foreign key(CUSTOMER_ID) REFERENCES CUSTOMER1 (CUSTOMER_ID) ON DELETE CASCADE,
foreign key(SALESMAN_ID) REFERENCES SALESMAN (SALESMAN_ID) ON DELETE CASCADE);



INSERT INTO SALESMAN VALUES (1000, 'JOHN', 'BANGALORE', '25 %');
INSERT INTO SALESMAN VALUES (2000, 'RAVI', 'BANGALORE', '20 %');
INSERT INTO SALESMAN VALUES (3000, 'KUMAR','MYSORE',    '15 %');
INSERT INTO SALESMAN VALUES (4000, 'SMITH','DELHI',     '30 %');
INSERT INTO SALESMAN VALUES (5000,'HARSHA','HYDRABAD',  '15 %');

INSERT INTO CUSTOMER1 VALUES (10, 'PREETHI','BANGALORE', 100, 1000);
INSERT INTO CUSTOMER1 VALUES (11, 'VIVEK',  'MANGALORE', 300, 1000);
INSERT INTO CUSTOMER1 VALUES (12, 'BHASKAR','CHENNAI', 400, 2000);
INSERT INTO CUSTOMER1 VALUES (13, 'CHETHAN','BANGALORE', 200, 2000);
INSERT INTO CUSTOMER1 VALUES (14, 'MAMATHA','BANGALORE', 400, 3000);

ALTER TABLE ORDERS    
MODIFY ORD_DATE varchar(20);

INSERT INTO ORDERS VALUES (50, 5000, '04-MAY-17', 10, 1000);
INSERT INTO ORDERS VALUES (51, 450, '20-JAN-17', 10, 2000);
INSERT INTO ORDERS VALUES (52, 1000, '24-FEB-17', 13, 2000);
INSERT INTO ORDERS VALUES (53, 3500, '13-APR-17', 14, 3000);
INSERT INTO ORDERS VALUES (54, 550, '09-MAR-17', 12, 2000);

select * from SALESMAN;
select * from CUSTOMER1 ;
select * from ORDERS ;


SELECT GRADE, COUNT(DISTINCT CUSTOMER_ID)
FROM CUSTOMER1
GROUP BY GRADE
HAVING GRADE > (SELECT AVG(GRADE)
FROM CUSTOMER1
WHERE CITY='BANGALORE');


SELECT SALESMAN_ID, NAME 
FROM SALESMAN A 
WHERE 1 < (SELECT COUNT(*) 
FROM CUSTOMER1 
WHERE SALESMAN_ID=A.SALESMAN_ID);


SELECT SALESMAN.SALESMAN_ID, NAME, CUST_NAME, COMMISSION
FROM SALESMAN, CUSTOMER1
WHERE SALESMAN.CITY = CUSTOMER1.CITY
UNION
SELECT SALESMAN_ID, NAME, 'NO MATCH', COMMISSION
FROM SALESMAN
WHERE NOT CITY = ANY
(SELECT CITY
 FROM CUSTOMER1)
ORDER BY 2 DESC;



select s.salesman_id, s.name, c.cust_name, s.commission from salesman s, customer1 c 
where s.city = c.city and s.SALESMAN_ID=c.salesman_id
UNION 
select s.salesman_id, s.name, c.cust_name, s.commission from salesman s, customer1 c 
where s.city != c.city and s.SALESMAN_ID=c.salesman_id;


CREATE VIEW E_SALESMAN AS 
SELECT B.ORD_DATE, A.SALESMAN_ID, A.NAME
FROM SALESMAN A, ORDERS B
WHERE A.SALESMAN_ID = B.SALESMAN_ID
AND B.PURCHASE_AMT=(SELECT MAX(PURCHASE_AMT)
FROM ORDERS C
WHERE C.ORD_DATE = B.ORD_DATE);

select * from E_SALESMAN;



delete from salesman where salesman_id=1000;
select * from salesman;





create database actor;
use actor;
create table actor (act_id int, act_name varchar(20), act_gender char, primary key(act_id));
create table director (dir_id int, dir_name varchar(20), dir_phone long, primary key(dir_id));
create table movies (mov_id int not null, mov_title varchar(20), mov_year int, mov_lang varchar(20), 
dir_id int, foreign key(dir_id) references director(dir_id), primary key(mov_id));
create table movie_cast (act_id int, mov_id int, role varchar(20), foreign key(act_id) references actor(act_id),
foreign key(mov_id) references movies(mov_id), unique(act_id, mov_id));
create table rating (mov_id int unique, rev_stars int, foreign key(mov_id) references movies(mov_id));

INSERT INTO ACTOR VALUES (301,'ANUSHKA','F');
INSERT INTO ACTOR VALUES (302,'PRABHAS','M');
INSERT INTO ACTOR VALUES (303,'PUNITH','M');
INSERT INTO ACTOR VALUES (304,'JERMY','M');

INSERT INTO DIRECTOR VALUES (60,'RAJAMOULI', 8751611001);
INSERT INTO DIRECTOR VALUES (61,'HITCHCOCK', 7766138911);
INSERT INTO DIRECTOR VALUES (62,'FARAN', 9986776531);
INSERT INTO DIRECTOR VALUES (63,'STEVEN SPIELBERG', 8989776530);

INSERT INTO MOVIES VALUES (1001,'BAHUBALI-2', 2017, 'TELAGU', 60);
INSERT INTO MOVIES VALUES (1002,'BAHUBALI-1', 2015, 'TELAGU', 60);
INSERT INTO MOVIES VALUES (1003,'AKASH', 2008, 'KANNADA', 61);
INSERT INTO MOVIES VALUES (1004,'WAR HORSE', 2011, 'ENGLISH', 63);

INSERT INTO MOVIE_CAST VALUES (301, 1002, 'HEROINE');
INSERT INTO MOVIE_CAST VALUES (301, 1001, 'HEROINE');
INSERT INTO MOVIE_CAST VALUES (303, 1003, 'HERO');
INSERT INTO MOVIE_CAST VALUES (303, 1002, 'GUEST');
INSERT INTO MOVIE_CAST VALUES (304, 1004, 'HERO');

INSERT INTO RATING VALUES (1001, 4);
INSERT INTO RATING VALUES (1002, 2);
INSERT INTO RATING VALUES (1003, 5);
INSERT INTO RATING VALUES (1004, 4);

show tables;
select * from actor;
select * from director;
select * from movies;
select * from movie_cast;
select * from rating;


select mov_title from movies where dir_id in (select dir_id from director 
where dir_name = 'HITCHCOCK');


select mov_title from movies M, movie_cast V where M.mov_id = V.mov_id and V.act_id 
in (select act_id from movie_cast group by act_id Having count(act_id)>1);


SELECT MOV_TITLE
FROM MOVIES M, MOVIE_CAST MV 
WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN (SELECT ACT_ID
FROM MOVIE_CAST GROUP BY ACT_ID
HAVING COUNT(ACT_ID)>1)
GROUP BY MOV_TITLE
HAVING COUNT(*)>1;


select A.act_name, M.mov_title, M.mov_year from actor A
join movie_cast MC on A.act_id = MC.act_id
join movies M
on M.mov_id=MC.mov_id
where M.mov_year not between 2000 and 2015;


select mov_title, max(rev_stars)
from movies
inner join rating using(mov_id)
group by mov_title
Having max(rev_stars) > 0
order by mov_title;


update rating
set rev_stars=5
where mov_id in
(select mov_id from movies
where dir_id in
(select dir_id from director
where dir_name = 'STEVEN SPIELBERG'));

select * from rating;








    
    """)


