SQL Code for Byrnetube video Stored Procedures SQL Code for Byrnetube video
SQL for people table CREATE TABLE people ( pid INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(pid), first VARCHAR(15) NOT NULL, last VARCHAR(20) NOT NULL, email VARCHAR(40) NOT NULL, ) ENGINE=INNODB;
SQL for password table CREATE TABLE passwords ( people_id INT NOT NULL, password VARCHAR(10), FOREIGN KEY people_id REFERENCES people (pid) ) ENGINE=INNODB;
SQL to add 2 people to person table INSERT INTO people (first, last, email) VALUES (‘Nicole’,’Polizzi’,’snooki@jerseyshore.com’) VALUES (‘Mike’,’Sorentino’,’situation@jerseyshore.com’) SELECT * FROM people
SQL to add passwords for people INSERT INTO passwords VALUES (‘1’,’gtl’) VALUES (‘2’,’abs’) SELECT * from passwords
SQL to print the database content SELECT * FROM people,passwords WHERE people.pid = password.people_id SELECT first, last, email, password
Query database for first and password SELECT first, password FROM people,passwords WHERE people.pid = password.people_id AND people.email=‘snooki@jerseyshore.com’
Stored Procedure – get first and password CREATE PROCEDURE getFirstPasswordUsingEmail ( IN inputEmail VARCHAR(40) ) BEGIN SELECT first, password FROM people,passwords WHERE people.pid = password.people_id AND people.email=inputEmail END CALL getFirstPasswordUsingEmail(‘situation@jerseyShore.com’)
Stored Procedure – create account CREATE PROCEDURE createAccount ( IN inputFirst VARCHAR(40) IN inputLast VARCHAR(40) IN inputEmail VARCHAR(40) IN inputPassword VARCHAR(40) ) BEGIN DECLARE generatedPid INT INSERT INTO people (first, last, email) VALUES (inputFirst, inputLast, inputEmail) SELECT pid INTO generatedPid FROM people where email=inputEmail INSERT INTO password (people_id, password) VALUES (generatedPid, inputPassword) END