NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Query to copy:

drop table genre_codes;
drop table format_types;
drop table video_stores;
drop table condition_codes;
drop table actors;
drop table movies;
drop table movie_cast;
CREATE TABLE genre_codes(
genre_code int IDENTITY(1,1) NOT NULL,
genre_code_description varchar(32) NOT NULL);

/* format_types */
CREATE TABLE format_types(
format_type_code int IDENTITY(1,1) NOT NULL,
format_type_description varchar(32) NOT NULL);
/* video_stores */
CREATE TABLE video_stores(
store_id int IDENTITY(1,1) NOT NULL,
store_name varchar(32) NOT NULL,
store_address varchar(128) NOT NULL,
store_city varchar(32) NOT NULL,
store_state varchar(2) NOT NULL,
store_zip varchar(12) NOT NULL,
store_email varchar(128) NOT NULL,
other_store_details varchar(512) NOT NULL);
/* condition_code */
CREATE TABLE condition_codes(
condition_code int IDENTITY(1,1) NOT NULL,
condition_code_description varchar(32) NOT NULL);
/* actors */
CREATE TABLE actors(
actor_id int IDENTITY(1,1) NOT NULL,
actor_gender char(1) NOT NULL,
actor_first_name varchar(32) NOT NULL,
actor_last_name varchar(32) NOT NULL,
other_actor_details varchar(512) NOT NULL);
/* movies */
CREATE TABLE movies(
movie_id int IDENTITY(1,1) NOT NULL,
condition_code int NOT NULL,
format_type_code int NOT NULL,
genre_type_code int NOT NULL,
store_id int NOT NULL,
release_year int NOT NULL,
movie_title varchar(128) NOT NULL,
movie_description varchar(1024) NOT NULL,
number_in_stock int NOT NULL,
rental_or_sale_or_both tinyint NOT NULL,
rental_daily_rate money NOT NULL,
sales_price money NOT NULL);
/* movie_cast */
CREATE TABLE movie_cast(
movie_id int NOT NULL,
actor_id int NOT NULL);


/* create primary keys with ALTER TABLE statement */
ALTER TABLE genre_codes ADD CONSTRAINT pk_genre_codes PRIMARY KEY (genre_code);
ALTER TABLE format_types ADD CONSTRAINT pk_format_types PRIMARY KEY (format_type_code);
ALTER TABLE video_stores ADD CONSTRAINT pk_video_stores PRIMARY KEY (store_id);
ALTER TABLE condition_codes ADD CONSTRAINT pk_condition_codes PRIMARY KEY (condition_code);
ALTER TABLE actors ADD CONSTRAINT pk_actors PRIMARY KEY (actor_id);
ALTER TABLE movies ADD CONSTRAINT pk_movies PRIMARY KEY (movie_id);
ALTER TABLE movie_cast ADD CONSTRAINT pk_movie_cast PRIMARY KEY (movie_id, actor_id);
/* end of primary key creation */

/* create foreign keys */
ALTER TABLE movie_cast ADD CONSTRAINT fk_Movie_cast_actors FOREIGN KEY(actor_id)
REFERENCES actors (actor_id);
ALTER TABLE movie_cast ADD CONSTRAINT fk_movie_cast_movies FOREIGN KEY(movie_id)
REFERENCES movies (movie_id);
ALTER TABLE movies ADD CONSTRAINT fk_movies_condition_codes FOREIGN KEY(condition_code)
REFERENCES condition_codes (condition_code);
ALTER TABLE movies ADD CONSTRAINT fk_movies_format_types FOREIGN KEY(format_type_code)
REFERENCES format_types (format_type_code);
ALTER TABLE movies ADD CONSTRAINT fk_movies_genre_codes FOREIGN KEY(genre_type_code)
REFERENCES genre_codes (genre_code);
ALTER TABLE movies ADD CONSTRAINT fk_movies_video_Stores FOREIGN KEY(store_id)
REFERENCES video_Stores (store_id);


/* Insert data for table genre_codes */


INSERT INTO genre_codes VALUES ('Action & Adventure');
INSERT INTO genre_codes VALUES ('Comedy');
INSERT INTO genre_codes VALUES ('Documentary');
INSERT INTO genre_codes VALUES ('Drama');
INSERT INTO genre_codes VALUES ('Education');
INSERT INTO genre_codes VALUES ('Foreign');
INSERT INTO genre_codes VALUES ('Horror');
INSERT INTO genre_codes VALUES ('Romance');
INSERT INTO genre_codes VALUES ('Thriller');
INSERT INTO genre_codes VALUES ('Western');

/* Insert data for table format_types */

INSERT INTO format_types values ('DVD');
INSERT INTO format_types values ('Blu-Ray');
INSERT INTO format_types values ('MiniDV');
INSERT INTO format_types values ('VHS');

/* Insert data for table video_stores */

INSERT INTO video_stores values ('FLIX2YOU Headquarters', '1225 W Linden Street', 'Scranton',
'PA', '18501', '[email protected]', 'Headquarters');
INSERT INTO video_stores values ('Pittsburgh Shadyside', '4125 S Highland Ave', 'Pittsburgh',
'PA', '15232', '[email protected]', 'Main Pittsburgh Store');

/* Insert data for table condition_codes */

INSERT INTO condition_codes VALUES ('rental');
INSERT INTO condition_codes VALUES ('used for sale');
INSERT INTO condition_codes VALUES ('damaged');

/* Insert data for moives, actors and move_cast */

/* FORMAT of the insert command ---> INSERT INTO movies VALUES (condition, format, genre, store, year,
'Title', 'Desc', num, rental/sale/both, daily, sale); */

/* Amazing Spider-Man */
INSERT INTO movies VALUES (1, 1, 1, 1, 2012, 'The Amazing Spider-Man',
'The story of Peter Parker, an outcast high schooler who was abandoned by his parents as a boy, leaving him to be raised by his Uncle Ben and Aunt May. Like most teenagers, Peter is trying to figure out who he is and how he got to be the person he is today. Peter is also finding his way with his first high school crush, Gwen Stacy, and together, they struggle with love, commitment, and secrets. As Peter discovers a mysterious briefcase that belonged to his father, he begins a quest to understand his parents disappearance - leading him directly to Oscorp and the lab of Dr. Curt Connors, his fathers former partner. As Spider-Man is set on a collision course with Connors alter-ego, The Lizard, Peter will make life-altering choices to use his powers and shape his destiny to become a hero.',
100, 1, 1.99, 25.39);
INSERT INTO actors values ('M', 'Andrew', 'Garfield',
'Although born in Los Angeles, Andrew Garfield grew up in England; his mother is English and they moved back there when he was three years old. He went to a private school, the City of London Freemen School, and began acting in youth theatre productions while he was still at school. At 19, he went to the Central School of Speech and Drama.');
INSERT INTO actors values ('F', 'Emma', 'Stone',
'Stone began acting as a child as a member of the Valley Youth Theatre in Phoenix, Arizona, where she made her stage debut in a production of Kenneth Grahames The Wind in the Willows. She appeared in many more productions through her early teens until, at the age of 15, she decided that she wanted to make acting her career.');
INSERT INTO movie_cast VALUES (1,1);
INSERT INTO movie_cast VALUES (1,2);

/* Superbad */
INSERT INTO movies VALUES (1, 1, 2, 2, 2007, 'Superbad',
'Two co-dependent high school seniors are forced to deal with separation anxiety after their plan to stage a booze-soaked party goes awry.',
25, 3, .99, 10.99);
INSERT INTO actors values ('M', 'Michael', 'Cera',
'Canadian actor Michael Cera is the middle child of a Canadian mother and Italian father, both of whom worked for Xerox. He has two sisters. He was educated at Conestoga Public School, Robert H. Lagerquist Senior Public School and Heart Lake Secondary School until the ninth grade. Cera then completed his high school education via correspondence.');
INSERT INTO actors values ('M', 'Jonah', 'Hill',
'Hill grew up in Los Angeles, the son of a tour accountant for Guns N Roses. He graduated from Crossroads School in Santa Monica and went on to The New School in New York to study drama.')
INSERT INTO actors values ('M', 'Christopher', 'Mintz-Plasse',
'Attended El Camino Real High School from 2003 to 2007. Because he was only 17 at the time of filming Superbad (2007), his mother was required to be on set during the filming of his sex scene.');
INSERT INTO movie_cast VALUES (2,3);
INSERT INTO movie_cast VALUES (2,4);
INSERT INTO movie_cast VALUES (2,5);


/* Gone with the wind */
INSERT INTO movies VALUES (2, 4, 4, 2, 1939, 'Gone with the Wind',
'American classic in which a manipulative woman and a roguish man carry on a turbulent love affair in the American south during the Civil War and Reconstruction.',
4, 2, .99, 4.99);
INSERT INTO actors values ('M', 'Clark', 'Gable',
'Clark Gables mother died when he was seven months old. At 16 he quit high school, went to work in an Akron Ohio tire factory and decided to become an actor after seeing the play The Bird of Paradise.');
INSERT INTO actors values ('F', 'Vivien', 'Leigh',
'If a film were made of the life of Vivien Leigh, it would open in India just before World War I, where a successful British businessman could live like a prince. In the mountains above Calcutta, a little princess is born. Because of the outbreak of World War I, she is six years old the first time her parents take her to England.');
INSERT INTO actors values ('M', 'Thomas', 'Mitchell',
'Thomas Mitchell was one of the great American character actors, whose credits read like a list of the greatest films of the 20th century');
INSERT INTO movie_cast VALUES (3,6);
INSERT INTO movie_cast VALUES (3,7);
INSERT INTO movie_cast VALUES (3,8);

/* Fahrenheit 9/11 */
INSERT INTO movies VALUES (1, 1, 3, 1, 2004, 'Fahrenheit 9/11',
'Michael Moores view on what happened to the United States after September 11; and how the Bush Administration allegedly used the tragic event to push forward its agenda for unjust wars in Afghanistan and Iraq.',
10, 3, .99, 11.99);
INSERT INTO actors values ('M', 'Michael', 'Moore',
'Michael Moore was born in Flint, Michigan April 23, 1954, but was not raised there. Contrary to popular belief, he was actually raised in Davison, Michigan. He studied journalism at the University of Michigan-Flint, and also pursued other hobbies such as gun shooting, for which he even won a competition.');
INSERT INTO actors values ('M', 'George', 'Bush',
'The 43rd President of the United States of America, George Walker Bush was born two days after the national holiday of the Fourth of July, 1946 in New Haven, Connecticut where his father was attending Yale College in the Class of 1949.');
INSERT INTO actors values ('M', 'Ben', 'Affleck',
'Benjamin Geza Affleck was born on August 15, 1972 in Berkeley, California, USA but raised in Cambridge, Massachusetts, USA. He was born to parents Tim Affleck, a social worker, who is now divorced from Bens mother Chris Affleck, a school teacher.');
INSERT INTO movie_cast VALUES (4,9);
INSERT INTO movie_cast VALUES (4,10);
INSERT INTO movie_cast VALUES (4,11);

/* The following command will cause the server to wait 5 seconds in order to be sure the server has committed the previous commands */
waitfor delay '0:0:5'


/* Good Will Hunting */
INSERT INTO movies VALUES (1, 1, 4, 2, 1997, 'Good Will Hunting',
'Will Hunting, a janitor at MIT, has a gift for mathematics but needs help from a psychologist to find direction in his life.',
6, 3, .59, 9.99);
INSERT INTO actors values ('M', 'Robin', 'Williams',
'Williams briefly studied political science before enrolling at Juilliard to study theatre. After he left Juilliard, he performed in night clubs where he was discovered for the role of Mork on an episode of Happy Days in 1974 and the subsequent Mork & Mindy TV series in 1978.');
INSERT INTO actors values ('M', 'Matt', 'Damon',
'Matt Damon was born in 1970. His father, Kent Damon, a tax preparer, and his mother, Nancy Carlsson-Paige, a college professor, are now divorced.');
INSERT INTO movie_cast VALUES (5,12);
INSERT INTO movie_cast VALUES (5,13);
INSERT INTO movie_cast VALUES (5,11);
*/

/*Customer_rentals*/
drop table customer_rentals;

CREATE TABLE customer_rentals (
item_rental_id int IDENTITY(1,1) NOT NULL,
customer_id int NOT NULL,
movie_id int NOT NULL,
rental_status_code int NOT NULL,
rental_date_out Date NOT NULL,
rental_date_returned date NOT NULL,
rental_amount_due money NOT NULL,
other_rental_details varchar(512) NOT NULL);
/* create primary keys with ALTER TABLE statement */

ALTER TABLE customer_rentals ADD CONSTRAINT pk_item1_rental_id PRIMARY KEY (item_rental_id);

INSERT INTO customer_rentals VALUES(1, 2, 5, '2017-10-02', '2017-11-02', 4.59, '10')
/*INSERT INTO customer_rentals VALUES(4, 3, 4, NULL, NULL, NULL, NULL)*/
INSERT INTO customer_rentals VALUES(3, 1, 6, '2017-08-03', '2017-11-02', 28.78, '10')
INSERT INTO customer_rentals VALUES(6, 4, 7, '2017-06-10', '2017-06-20', 8.60, '10')
INSERT INTO customer_rentals VALUES(5, 5, 5, '2017-07-10', '2017-11-02', 3.56, '10')

/* Part-1*/
ALTER TABLE MOVIES ADD num_rentals INT;
UPDATE MOVIES SET num_rentals = 0;
UPDATE MOVIES SET num_rentals=0 WHERE movie_id=4;
ALTER TABLE MOVIES ALTER COLUMN num_rentals INT NOT NULL;

/*Part-2*/

/* Create trigger for the INSERT event on customer_rentals */
DROP trigger movies_num_rentals_insert;
GO
CREATE TRIGGER movies_num_rentals_insert
on customer_rentals for insert as
BEGIN
UPDATE movies
SET num_rentals = num_rentals + 1
WHERE movie_id = (SELECT movie_id from INSERTED)
Select movie_id from inserted
END;

/* Create trigger for the DELETE event on customer_rentals */
DROP trigger movies_num_rentals_delete;
GO
CREATE TRIGGER movies_num_rentals_delete
on customer_rentals for DELETE
as
BEGIN
UPDATE movies
SET num_rentals = num_rentals - 1
WHERE movie_id = (SELECT movie_id from DELETED)
END;

/* Create trigger for the UPDATE event on customer_rentals */
DROP trigger movies_num_rentals_update;
GO
CREATE TRIGGER movies_num_rentals_update
on customer_rentals FOR UPDATE
as
BEGIN
UPDATE movies
SET num_rentals = num_rentals - 1
WHERE movie_id = (SELECT movie_id from DELETED);
UPDATE movies SET num_rentals = num_rentals + 1
WHERE movie_id = (SELECT movie_id from INSERTED);
END;

SELECT * FROM MOVIES;
SELECT * FROM customer_rentals;

/*TEST THE INSERT TRIGGER*/
/*Before insert the record*/
select movie_id, num_rentals from movies;
INSERT INTO customer_rentals VALUES(7, 3, 8, '2017-07-10', '2017-11-02', 3.56, '10')
/*After insert the record*/

/*Before insert the record*/
select movie_id, num_rentals from movies;
INSERT INTO customer_rentals VALUES(8, 5, 9, '2017-07-10', '2017-11-02', 3.56, '12')
/*After insert the record*/

select movie_id, num_rentals from movies;
/*Before insert the record*/
INSERT INTO customer_rentals VALUES(9, 9, 10, '2017-07-10', '2017-11-02', 3.56, '14')
/*After insert the record*/
select movie_id, num_rentals from movies;

/*TEST THE UPDATE TRIGGER*/
UPDATE customer_rentals set other_rental_details = 12 WHERE item_rental_id = 7;
select movie_id, num_rentals from movies;

/*TEST the DELTE TRIGGER*/
DELETE FROM customer_rentals where item_rental_id = 7;
select movie_id, num_rentals from movies;
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.