Professional Documents
Culture Documents
Learning SQL: Chapter 2 - Creating and Populating A Database
Learning SQL: Chapter 2 - Creating and Populating A Database
Learning SQL
Chapter 2 – Creating and Populating a Database
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
What This PowerPoint Covers:
• Learn how to use the MySQL CLI.
• Learn how to create a database, implement a database design and
populate tables with data.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
MySQL Command-Line Tool
Open a Windows or Unix shell and type:
mysql –u root –p;
Enter your password, after which you will see the mysql> prompt.
To see all databases, type show databases.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 18
MySQL Command-Line Tool
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 19
Create a Database
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 27-29
Brainstorming
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 27
Column Names and Data Types
Varchar allows up to the determined amount of characters. The eye_color only
needs 2 chars, BL, BR, GR. Birth_date is a date, because it only needs month, day,
year.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 28
Refinement
• The name column is a compound object, meaning it contains first
name and last name.
• Multiple people have same name, eye color, etc. so there is no
uniqueness
• Address column is also a compound because it contains street, city, etc.
• Favorite food can contain zero, one or two items. Creating a separate
table for favorite food would be the best option.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 28-29
Updated Person Table Food Table
Column Type Allowable Values Column Type
person_id smallint (unsigned) person_id smallint
first_name varchar(20) food varchar(20)
last_name varchar(20)
eye_color char(2) BR, BL, GR
The person_id and food
birth_date date
columns comprise the
street varchar(30) primary key of the
city varchar(20) favorite_food table,
state varchar(20) and the person_id
column is also a foreign key
country varchar(20)
to the person table.
postal_code varchar(20)
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 29
Writing the Person Schema Statements
CREATE TABLE person Everything here should be self-
(person_id SMALLINT UNSIGNED, explanatory, except the last line. The
fname VARCHAR(20), last line is a primary key constraint,
lname VARCHAR(20), and it gives the person_id the
eye_color CHAR(2), primary key.
birth_date DATE,
street VARCHAR(20),
city VARCHAR(20),
state VARCHAR(20),
country VARCHAR(20),
postal_code VARCHAR(20),
CONSTRAINT pk_person PRIMARY KEY (person_id)
);
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 30
Writing the Schema Statements
There is another place we can use a constraint, except it will be a check constraint.
Remember how we only allowed three values in eye_color?
We can type:
eye_color CHAR(2) CHECK (eye_color IN (‘BR’, ‘BL, ‘GR’)),
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 30
Entering the Person Table Statement
mysql> CREATE TABLE person
After entering in the SQL statement, the
-> (person_id SMALLINT UNSIGNED,
-> fname VARCHAR(20),
return message “Query OK, 0 rows affected”
-> lname VARCHAR(20), shows that there are no errors.
-> eye_color ENUM(‘BR’, ‘BL’, ‘GR’),
-> birth_date DATE,
-> street VARCHAR(20),
-> city VARCHAR(20),
-> state VARCHAR(20),
-> country VARCHAR(20),
-> postal_code VARCHAR(20),
-> CONSTRAINT pk_person PRIMARY KEY (person_id)
->);
Query OK, 0 rows affected (0.37 sec)
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 31
Entering the Food Table Statement
mysql> CREATE TABLE favorite_food Since a person can have more than one
-> (person_id SMALLINT UNSIGNED, favorite food, we need more than just
-> food VARCHAR(20), person_id for uniqueness. There is two
-> CONSTRAINT pk_favorite_food
-> PRIMARY KEY (person_id, food), primary keys – person_id and food.
-> CONSTRAINT fk_fav_food_person_id
-> FOREIGN KEY (person_id)
-> REFERENCES person (person_id) The second constraint makes sure that a
-> ); person_id that does not exist in the
Query OK, 0 rows affected (0.10 sec) person table cannot be added in the
food table.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 32
Auto-Increment Primary Key
The primary key needs to be unique, and fortunately SQL allows an auto-
incrementation.
ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
When populating the table, the first value entered should be null. This is because the
table will automatically auto-increment it.
mysql> INSERT INTO person
-> (person_id, fname, lname, eye_color, birth_date)
-> VALUES (null, ‘William’, ‘Turner’, ‘BR’, ‘1972-05-27’);
Query OK, 1 row affected (0.22 sec)
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 34
Insert Into Food Table
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 33
Updating Data
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 38
Delete Data
mysql> DELETE FROM persons When using the WHERE, always use the primary
-> WHERE personid = 1; key. If the WHERE clause is not used, all rows will
Query OK, 1 row affected (0.01 sec) be deleted.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 38-39
Points to Remember
• Brainstorm, assign column names and data types, refine columns and
define primary key.
• CREATE, INSERT, ALTER,UPDATE, and DELETE
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.