You are on page 1of 18

CIT 225

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

To enter mysql and go to a database, type:


mysql –u root –p databaseName;
Replace databaseName with the name of the database you want to
view.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 19
Create a Database

• Brainstorm about what information is important


• Assign column names and data types
• Refine the data and assign a primary key

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 27-29
Brainstorming

We will create a table containing information about a person. The table


will contain the following information:
• Name
• Eye color
• Birth date
• Address
• Favorite Foods

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.

Column Type Allowable values


name varchar(48)
eye_color char(2) BL, BR, GR
birth_date date
address varchar(100)
favorite_foods varchar(200)

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’)),

However, this does not enforce it, only defines.


MySQL has another type called enum that combines the data type and check constraint.
Instead, we can write:
eye_color ENUM(‘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

mysql> INSERT INTO favorite_food


-> (person_id, food)
-> VALUES(1, 'pizza');
mysql> INSERT INTO favorite_food
-> (person_id, food)
-> VALUES(1, 'cookies');

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc. Learning SQL – page 33
Updating Data

mysql> UPDATE person If you wanted to update more than 1


-> SET street = '1225 Tremont St. ', row, you could write:
-> city = 'Boston', WHERE person_id < 10;
-> state = 'MA', If there was another person in the
-> country = 'USA’, table, both people would have the
-> postal_code = '02138’ same address because both
-> WHERE person_id = 1; person_ids would be less than
10.

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.

You might also like