CSCI-UA:0060-02 Database Design & Web Implementation Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com Lecture #15: Post Spring Break Massive MySQL Review Database Design and Web Implementation
Database Design and Web Implementation 2
Administrivia Midterm Readings: Read through chapter 10 if you haven t already Homework Homework 5 is due Thursday Database Design and Web Implementation 3
On Today s Menu Creating tables Inserting data Selecting Data Updating Data Deleting Data Database Design and Web Implementation 4
Create Table Syntax CREATE TABLE [table_name] ( [column_name] [column_type] [column_details], KEY([column_name(s)], UNIQUE KEY([column_name(s)], PRIMARY KEY([column_name(s)], FOREIGN KEY ([column_name]) REFERENCES [table_name]([column_name]) ) ENGINE=[engine] DEFAULT CHARSET=[charset] COLLATE=[collation]; Database Design and Web Implementation 5
Create Table Syntax [column_name] [column_type] [column_details] CREATE TABLE example ( example_id int(11) NOT AUTO_INCREMENT, example_col1 varchar(255) DEFAULT Evan, example_col2 timestamp NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); Database Design and Web Implementation 6
Create Table Syntax KEYS!! CREATE TABLE example ( example_id int(11) NOT AUTO_INCREMENT, example_col1 varchar(255) DEFAULT Evan, example_col2 timestamp NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY_KEY (example_id), UNIQUE KEY(example_col1), KEY(example_col2) ); Database Design and Web Implementation 7
Create Table Syntax Engine, Charset, Collate CREATE TABLE example ( example_id int(11) NOT AUTO_INCREMENT, example_col1 varchar(255) DEFAULT Evan, example_col2 timestamp NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci; Database Design and Web Implementation 8
Data Types Type {storage} Name Range Attributes Default Numeric {1 byte} Numeric {2 bytes} Numeric {3 bytes} Numeric {4 bytes} Numeric {8 bytes} Numeric {4 or 8} Numeric {4 bytes} Numeric {8 bytes} Numeric {M+2} Bit {8 bytes} TINYINT[(M)] -128 TO 127 [0 to 255 if UNSIGNED] SMALLINT[(M)] -32,768 to 32,767 [0 to 65,535] MEDIUMINT[(M)] -8,388,608 to 8,388,607 [0 to 16,777,215] INT[(M)] BIGINT[(M)] FLOAT(p) FLOAT[(M,D)] DOUBLE[(M,D)] DECIMAL[(M,[D])] Stored as string BIT[(M)] -2,147,483,648 to 2,147,483,647 [0 to 4,294,967,295] -/+9.223E+18 [0 to 18.45E+18] p=0-24 --> "FLOAT" p=25-53 --> "DOUBLE" Min=+/-1.175E-38 Max=+/-3.403E+38 Min=+/-2.225E-308 Max=+/-1.800E+308 Max Range = DOUBLE range Fixed point vs. DOUBLE float Binary. Display by [add zero converting with BIN()]. M=1-64 AUTO_INCREMENT UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL Prior to 5.03 TINYINT(1) Synonym Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] Database Design and Web Implementation 9
Data Types Type {storage} Name Range Attributes Default String {M char's} String {M char's1} String {#char's1} String {#char's1} String {#char's1} String {#char's1} CHAR[(M)] VARCHAR(M) M=0-255 Characters, FIXED. Right padded with spaces. M=0-65,535 Characters M=0-255 <v5.0.3 BINARY, CHARACTER SET BINARY, CHARACTER SET [ if NOT ] [ if NOT ] TINYTEXT2 0-255 Characters BINARY, CHARACTER SET ["" if NOT ] TEXT2 0-65,535 Char's BINARY, CHARACTER SET ["" if NOT ] MEDIUMTEXT2 0-16,777,215 Char's BINARY, CHARACTER SET ["" if NOT ] LONGTEXT2 0-4,294,967,295 Char's BINARY, CHARACTER SET ["" if NOT ] Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file Database Design and Web Implementation 10
Data Types Type {storage} Name Range Attributes Default String {M bytes} String {M bytes} String {#bytes1} String {#bytes1} String {#bytes1} String {#bytes1} String {1-2 bytes} BINARY[(M)] M=0-255 bytes, FIXED. Global Only (case sensitive) VARBINARY(M) 0-65,535 bytes M=0-255 <v5.0.3 Global Only (case sensitive) TINYBLOB 0-255 bytes Global Only (case sensitive) BLOB 0-65,535 bytes Global Only (case sensitive) MEDIUMBLOB 0-16,777,215 bytes Global Only (case sensitive) LONGBLOB 0-4,294,967,295 bytes Global Only (case sensitive) ENUM2 ("A1","A2",...) Column is exactly 1 of values (1-255 values) CHARACTER SET ["" if NOT ] ["" if NOT ] [ if NOT ] [ if NOT ] [ if NOT ] [ if NOT ] [ if NOT ] Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file Database Design and Web Implementation 11
Data Types Type {storage} Name Range Attributes Default Date & Time {3 bytes} Date & Time {8 bytes} Date & Time {3 bytes} Date & Time {4 bytes} Date & Time {1 bytes} DATE "1000-01-01" - "9999-12-31" Global Only (YYYY-MM-DD) DATETIME "1000-01-01 00:00:00" - "9999-12-31 23:59:59" TIME "-838:59:59" - "838:59:59" Global Only (hh:mm:ss) TIMESTAMP 19700101000000-2037+ Global Only (YYYY-MM-DD hh:mm:ss) Global Only (YYYYMMDDhhmmss) YEAR 1900-2155 Global Only (YYYY) ["0000-00 -00" if NOT ] ["0000-00 -00 00:00:00" if NOT ] ["00:00:00 " if NOT ] Current Date & Time ["0000" if NOT ] Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file Database Design and Web Implementation 12
Article Data { "authors": { "person": [ "Peter APPLEBOME" ] }, "body": "Christopher S. Murphy, a 39-year-old three-term congressman, defeated the former wrestling executive Linda E. McMahon in a long and bitter Senate race.", "headline": "Democrat Elected Senator in Costly Connecticut Race", "publication_date": "2012-11-07", "section": "U.S.", "tags": { "DES": [ "United States Politics and Government", "Voting and Voters" ], "GEO": [ "Connecticut" ], "ORG": [ "Senate" ], "PER": [ "Murphy, Christopher Scott", "McMahon, Linda E" ] } } Database Design and Web Implementation 13
Database Design and Web Implementation 14
CREATE TABLE CREATE TABLE `section` ( `section_name` VARCHAR(45), PRIMARY KEY (`section_name`) ) ENGINE = InnoDB; CREATE TABLE IF `author` ( `author_id` INT NOT AUTO_INCREMENT, `author_name` VARCHAR(255) NOT, `author_type` ENUM('PER','ORG') NOT, PRIMARY KEY (`author_id`) ) ENGINE = InnoDB; Database Design and Web Implementation 15
CREATE TABLE CREATE TABLE `tag` ( `tag_id` INT NOT AUTO_INCREMENT, `tag` VARCHAR(255) NOT, `tag_type` ENUM('PER','ORG','GEO','DES','TTL') NOT, PRIMARY KEY (`tag_id`) ) ENGINE = InnoDB; Database Design and Web Implementation 16
CREATE TABLE CREATE TABLE `article` ( `article_id` INT NOT AUTO_INCREMENT, `article_headline` VARCHAR(255) NOT, `article_publication_date` DATETIME NOT, `article_body` MEDIUMTEXT NOT, `section` VARCHAR(255) NOT, `article_correction` TEXT, PRIMARY KEY (`article_id`), FOREIGN KEY (`section` ) REFERENCES `section` (`section_name` )) ENGINE = InnoDB; Database Design and Web Implementation 17
CREATE TABLE CREATE TABLE `article_tag` ( `article_id` INT NOT, `tag_id` INT NOT, PRIMARY KEY (`article_id`, `tag_id`), FOREIGN KEY (`article_id` ) REFERENCES `article` (`article_id` ) FOREIGN KEY (`tag_id` ) REFERENCES `tag` (`tag_id` )) ENGINE = InnoDB; Database Design and Web Implementation 18
CREATE TABLE CREATE TABLE `article_author` ( `article_id` INT NOT, `author_id` INT NOT, PRIMARY KEY (`article_id`, `author_id`), FOREIGN KEY (`article_id` ) REFERENCES `article` (`article_id` ) FOREIGN KEY (`author_id` ) REFERENCES `author` (`author_id` )) ENGINE = InnoDB; Database Design and Web Implementation 19
INSERT Syntax INSERT INTO [table_name] (column_1, column_2,...) VALUES (value_1, value_2,...); Database Design and Web Implementation 20
INSERT Syntax INSERT INTO [table_name] (column_1, column_2,...) VALUES (value_1, value_2,...), (value_1, value_2,...), (value_1, value_2,...), (value_1, value_2,...), (value_1, value_2,...); Database Design and Web Implementation 21
Inserting Test Data Insert test article, tag, author and section. Database Design and Web Implementation 22
Loading Data LOAD DATA LOCAL INFILE "[file_name]" INTO TABLE [table_name]( [column_1], [column_2],... [column_n] ); Database Design and Web Implementation 23
Loading Data - From CSV LOAD DATA LOCAL INFILE "[file_name]" INTO TABLE [table_name]( [column_1]... [column_n] ) FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' Database Design and Web Implementation 24
Simple Select Statement SELECT * FROM table LIMIT x OFFSET y Database Design and Web Implementation 25
Simple Select Statement SELECT row_1, row_2,... FROM table_1, table_2,... WHERE condition_1 (AND OR) condition_2 (AND OR)... Database Design and Web Implementation 26
Simple Select Statement with order by SELECT row_1, row_2,... FROM table_1, table_2,... WHERE condition_1 (AND OR) condition_2 (AND OR) ORDER BY column_1, column_2 Database Design and Web Implementation 27
Aggregate Queries SELECT SUM(numeric_column_1), MIN(numeric_column_2), MAX(numeric_column_3), AVG(numeric_column_4), COUNT(numeric_column_5), COUNT(DISTINCT numeric_column_1), FROM table_1... Database Design and Web Implementation 28
Aggregate Queries With Group By SELECT SUM(numeric_column_1), other_column_1, FROM table_1 GROUP BY other_column_1 Database Design and Web Implementation 29
Select Join Syntax SELECT * FROM table_1 JOIN table_2 ON table_1.id = table_2.id Database Design and Web Implementation 30
Select Alternate Join Syntax SELECT * FROM table_1, table_2 WHERE table_1.id = table_2.id Database Design and Web Implementation 31
Select Alternate Join Syntax SELECT * FROM table_1, table_2, table_3 WHERE table_1.id = table_2.id AND table_2.id = table_3.id Database Design and Web Implementation 32
Select CONCAT SELECT CONCAT(column_1,glue_1,column_2) FROM table_1; Database Design and Web Implementation 33
Select Subselect SELECT column_1 FROM table_1; WHERE column_n < (SELECT FUNC(*) FROM table); Database Design and Web Implementation 34
Query Me This How many tags are there of each type? How many authors are there of each type? Find the article with the most tags? Find the most commonly applied tag? How many articles are authored by more than one author? How many articles are authored by organizations services? Database Design and Web Implementation 35
UPDATE Syntax UPDATE [table_name] SET [column_1] = [value_1] [column_2] = [value_2] WHERE [condition_1] [(AND OR) condition_2] [(AND OR) condition_3]... [(AND OR) condition_n] Database Design and Web Implementation 36
DELETE Syntax DELETE FROM [table_name] WHERE [condition_1] [(AND OR) condition_2] [(AND OR) condition_3]... [(AND OR) condition_n] Database Design and Web Implementation 37