250 likes | 373 Views
CIS 218 Database Implementation. Three Stages of Database Development. Requirements Design Implementation. MySQL. An open-source DBMS Can be accessed via: the command line phpMyAdmin a custom interface Popular for web applications. Using the Command Line. Launch WampServer
E N D
Three Stages ofDatabase Development • Requirements • Design • Implementation
MySQL • An open-source DBMS • Can be accessed via: • the command line • phpMyAdmin • a custom interface • Popular for web applications
Using the Command Line • Launch WampServer • Open the MySQL console • Press Enter at the password prompt
Creating a Database • CREATE DATABASE dbname; > CREATE DATABASE hcc; • USE dbname; > USE hcc;
Creating a Table • CREATE TABLE tblname( column_specs ); CREATE TABLE hccStudent( sid CHAR(11) NOT NULL,stuLast VARCHAR(20) NOT NULL,stuFirst VARCHAR(20) NOT NULL, birthdate DATE NULL, PRIMARY KEY (sid)) ENGINE = InnoDB;
IF NOT EXISTS • CREATE TABLE IF NOT EXISTS tblname( column_specs ); • Checks to see if the table already exists, and only creates it if it does not • Prevents errors for tables that have already been created
Storage Engines • MySQL supports multiple storage engines • Engine determines certain table characteristics • InnoDB • Supports referential integrity • Supports cascade updates and deletes • Supports transactions • Supports row-level locking • SHOW ENGINES; displays available engines • Default engine is InnoDB
MySQL Data Types • CHAR – fixed length string, 0-255 • VARCHAR – variable string, 0-255 • TEXT – strings up to 65535 characters • BLOB – binary data, e.g. images • INT – whole numbers • DECIMAL – floating point numbers • DATE – YYYY-MM-DD • DATETIME – YYYY-MM-DD HH:MM:SS
Naming Rules • Identifiers are the names of databases, tables, and columns • Identifiers may contain any alphanumeric character plus ‘_’ or ‘$’ • Identifiers can start with any character that is legal in an identifier • An identifier may not consist entirely of digits
Naming Rules • Table and database names can be quoted (delimited) with backticks ( ` ) • CREATE TABLE `Employees` • Required only when identifiers contain spaces, which should be avoided • CREATE TABLE `Full Time Employees` • Period (.) and slashes (/, \) are never allowed • Identifiers can be up to 64 characters long
Inserting & Selecting Data • INSERT INTO tblname VALUES (values); • INSERT INTO hccStudent VALUES ('123-45-6789', 'Simpson', 'Homer', '1947-5-5'); • INSERT INTO hccStudent VALUES ('456-78-9809', 'Simpson', 'Bart', NULL); • INSERT INTO hccStudent VALUES ('555-66-7890', 'Bird', 'Tweety', '10-10-2013'); • SELECT * FROM hccStudent; Invalid date format; what happens?
Alternative Syntax • INSERT INTO tblname (columns) VALUES (values); • Less dependent on database structure INSERT INTO hccStudent (sid, stuLast, stuFirst) VALUES ('123-45-6789', 'Simpson', 'Homer'); Note that birthdate is optional. What happens if you try to omit stuFirst?
Useful Statements • DESCRIBE tblname; • or DESC or EXPLAIN • SHOW TABLES; • SHOW CREATE TABLE tblname; • SHOW DATABASES; • source filename; • DROP TABLE [IF EXISTS] tblname; • DROP DATABASE dbname;
Auto Increment • Used to automatically increment a PK • Surrogate key • Has no meaning to the user • CREATE TABLE hccAdvisor (advisorID INT NOT NULL AUTO_INCREMENT,advisorFirst VARCHAR(30) NOT NULL, • advisorLast VARCHAR(30) NOT NULL, • advisorExt VARCHAR(4), • PRIMARY KEY (advisorID) • )ENGINE=InnoDB;
hccAdvisor advisorID advisorFirst advisorLast advisorExt Inserting Rows • Use NULL in place of a value for surrogate keys (AUTO_INCREMENT) INSERT INTO hccAdvisorVALUES (NULL, 'Tina', 'Ostrander', '6040'); • List columns, but leave out primary key INSERT INTO hccAdvisor (advisorFirst, advisorLast, advisorExt) VALUES ('Tina', 'Ostrander', '6040');
Foreign Keys • A Student can have more than one Score hccStudent sid stuLast studFirst birthdate hccScore scoreID score type comment sid • CREATE TABLE hccScore (scoreID INT NOT NULL AUTO_INCREMENT, score DECIMAL(4, 2), • type CHAR(1), • comment TEXT, • sid CHAR(11), • PRIMARY KEY (scoreID), • FOREIGN KEY (sid) REFERENCES hccStudent(sid) • )ENGINE=InnoDB;
Referential Integrity • Every foreign key value must have a matching primary key value or else be null. INSERT INTO hccScoreVALUES (NULL, 9.5, 'Q', '123-45-6789', NULL); INSERT INTO hccScoreVALUES (NULL, 88.0, 'T', '333-44-5555', NULL); hccScore scoreID score type sid comment This value is NOT in the parent table!
Minimum Cardinality • If a parent entity is optional (minimum = 0), then the foreign key column should be optional hccAdvisor advisorID advLast advFirst advEmail hccStudent sid stuLast studFirst birthdate advisorID Foreign key is optional(NULL)
Minimum Cardinality • If a parent entity is required (minimum = 1), then the foreign key column should be required hccAdvisor advisorID advLast advFirst advEmail hccStudent sid stuLast studFirst birthdate advisorID Foreign key is required(NOT NULL)
Escaping Quotes When a string value contains a quote, it needs to be escapedINSERT INTO hccScoreVALUES (NULL, 97, 'P', '880359176', 'This was John\'s best paper.');
Practice • Create a “ratings” table to store restaurant ratings • Make the primary key auto-increment • Make the restaurant name required ratings ratingID INT name VARCHAR(30) address VARCHAR(50) city VARCHAR(30) price VARCHAR(30) rating INT(1) comments TEXT reviewDate DATE
CREATE TABLE ratings( ratingID INT NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT NULL, address VARCHAR(50) NULL, city VARCHAR(30) NULL, price VARCHAR(30) NULL, rating INT(1) NULL, comments TEXT, reviewDate DATE, PRIMARY KEY (ratingID) )ENGINE=InnoDB;
Practice • Populate the “ratings” table Lakeside 123 Westlake Seattle Price: High Rating: 5 Comments: Good food, great service 1-5-2010 Trapper’s 97 Wax Rd. Covington Price: Medium Rating: 5 Comments: Best sushi EVER! 3-10-2010 Applebee’s 3432 104th Kent Price: Medium Rating: 1 Comments: Bleck! 4-19-2010
INSERT INTO ratings VALUES(NULL, 'Lakeside', '123 Westlake', 'Seattle', 'High', 5, 'Good food, great service', '2010-01-05'); INSERT INTO ratings VALUES(NULL, 'Trapper\'s', '97 Wax Rd.', 'Covington', 'Medium', 5, 'Best sushi EVER!', '2010-03-10'); INSERT INTO ratings VALUES(NULL, 'Applebee\'s', '3432 104th', 'Kent', 'Medium', 5, 'Bleck!', '2010-04-19');