You are on page 1of 33

Upcoming

• Quiz 2 on 9/20 (next week)

• Homework 2 will be released this week, due on


9/25 by 11:59 PM

1
XAMPP
• There are many options for database development
environments. XAMPP is one of the most popular,
industry-standard options

X – cross-platform (Windows, Mac, Linux)


A – Apache (web server software)
M – MariaDB (database software – a
popular variant of MySQL)
P – PHP (server-side web programming)
P – Perl (server-side web programming)
2
MySQL
• MySQL is a free and open-source relational database
management system (RDBMS) created by Oracle
• Oracle also has a different but very similar RDBMS for
huge deployments, which is paid software

• One of the most popular RDMBS in industry

• Used by companies such as Facebook, Twitter, and


Google

3
XAMPP startup
Windows Mac

• Start “XAMPP Control • Start “manager-osx.app”


Panel”

4
XAMPP startup
Windows Mac

• Start “Apache” and • Go to “Manage Servers”


“MySQL” • Start “Apache Web Server”
and “MySQL Database”

5
XAMPP startup
• In your favorite web browser, navigate to
localhost/phpmyadmin (and maybe consider
bookmarking this page)

6
Creating a new database
Through phpMyAdmin Via SQL
• Click on “New” • Click on “SQL”

• Type the name of the • Use the following


new database, and press command:
“Create” CREATE DATABASE name;

7
Managing databases in SQL
• One database can contain many tables. Multiple
databases may be used if each has a different theme,
such as one database for sales and another for HR

• Creating a database can cause an error if the


database already exists. This can be resolved by:
CREATE DATABASE IF NOT EXISTS name;

• SQL can also be used to delete databases (cannot be


undone!):
DROP DATABASE IF EXISTS name;
8
Text data types
• CHAR(size) is a data type for storing text

• Text can consist of letters, numbers, and special


characters, e.g., “I ate 3 apples & 4 pears!”

• CHAR(5) would mean that each cell stores 5 characters


of text. Any characters beyond the first 5 are truncated.
Storing 4 or fewer characters of text would result in
padding with spaces, e.g., “cat “

• Best for data of fixed size. For example, zip codes are
always 5 characters long
9
Text data types
• VARCHAR(size) is another data type for storing text

• Text can consist of letters, numbers, and special


characters, e.g., “I ate 3 apples & 4 pears!”

• VARCHAR(5) would mean that each cell stores up to 5


characters of text. Any characters beyond the first 5 are
truncated. Storing 4 or fewer characters of text is
allowed with no padding/spaces

• Behind the scenes, VARCHAR stores both the text itself


and the number of characters (e.g., “pizza”, 5
characters). If the length is fixed, then CHAR takes up
less space
10
Numeric data types
• INT is a data type for whole (integer) numbers
• Can be positive or negative, e.g., 1958 or -67

• DECIMAL() is a data type for precise decimal numbers


• Can be positive or negative, e.g., -1.71 or 6.521
• DECIMAL(5,2) means up to 5 total digits, and up to 2 decimal
places. So, -999.99 to +999.99

• FLOAT is a data type for approximate decimal numbers


• Uses a shortcut to store numbers in less space than
DECIMALS
11 • May lose precision; e.g., 1.2 may become 1.19999
Temporal data types
• DATE stores dates in format YYYY-MM-DD, e.g.,
2021-08-30

• TIME stores times in format HH:MM:SS. Seconds


can be fractional, e.g., 08:01:12.567

• DATETIME stores both a date and a time in one


field, e.g., 2021-08-30 08:01:12.567

12
Creating tables through phpMyAdmin
• Choose a database and select “Structure.” Name the
table, select the number of columns and press “Go”:

• Fill out the details for each field:

13
Creating tables in SQL
• The most basic SQL to create a table looks like
(indentation optional):
Name of the table
CREATE TABLE IF NOT EXISTS Persons (
PersonID INT,
Names and data
LastName VARCHAR(255), types of each field
FirstName VARCHAR(255),
PRIMARY KEY (PersonID)
); Primary key field (note: composite
primary keys are comma-separated, such
14
as PRIMARY KEY (Title, DateOfRelease)
Working with phpMyAdmin tables
• To view/edit data in a table, select it and then select
“Browse”

15
Working with phpMyAdmin tables
• To view/edit the structure of a table, select it and
then select “Structure”

16
Working with phpMyAdmin tables
• To insert new data into a database, select it and then
select “Insert”

17
Deleting a table
Through phpMyAdmin Via SQL
• Click on “Drop” next to • Click on “SQL”
the table you’d like to
delete

• Use the following


command:
DROP TABLE IF EXISTS name;

18
Creating a table example
• Transform this ER diagram entity into a database
table:
Student Units
ID taken

Name Students

Address Birthdate

19 Week 4 Example 1
ID fields in SQL
• To create an ID field, add AUTO_INCREMENT after an
integer field

CREATE TABLE IF NOT EXISTS Persons (


PersonID INT AUTO_INCREMENT,
Name VARCHAR(500), By default, this would start
at 1. To change the value,
PRIMARY KEY (PersonID) also run:
); ALTER TABLE Persons
AUTO_INCREMENT = 100;

20
Creating a table example
• Transform this ER diagram entity into a database
table:

Name

Supplier
Industry Suppliers
ID

Email

21 Week 4 Example 2
Adding data to databases
• Data can be added one row at a time using an
INSERT query:
INSERT INTO Persons (ID, Name, Address)
VALUES (7, “John Smith”, “123 Main Street”)

• If entering a value for every field in the same order


as the fields in the table, then we don’t need to
specify the fields:
INSERT INTO Persons
VALUES (7, “John Smith”, “123 Main Street”)
22
Adding data to databases
• We can also insert multiple values at once if we
separate each new row with commas:
INSERT INTO Persons (ID, Name, Address)
VALUES
(7, “John Smith”, “123 Main Street”),
(8, “Aaron Young”, “456 East Lane”),
(9, “Max Jones”, “789 West Avenue”)

23
Adding data to databases
• Data can be added in bulk from files, if available. In
phpMyAdmin, select “Import” and choose a file

• Specify the column names/ordering if necessary:

24
Exporting data from databases
• Similarly, data can be exported from a database to
common file formats from the “Export” tab

25
Inserting data example
• Add “San Diego International Airport” with latitude
32.7338, longitude 117.1933 to the airports table.
Also, import bulk data from airports.csv

Latitude

Airport Airport
Airports
name ID

Longitude

26 Week 4 Example 3 airports.csv


Altering a table
• A table can be updated (altered) after it has been
created

• If a new field is created, that field will be filled with


NULL (blank) values by default

• We can also change the data type of a field if the


new data type is related to the former data type. For
example, changing CHAR(5) to VARCHAR(5) would
not be a problem
27
Altering a table
• Adding a field:
ALTER TABLE Persons
ADD DateOfBirth DATE;
Changes the data type
• Modifying a field: for DateOfBirth to
DATETIME
ALTER TABLE Persons
MODIFY COLUMN DateOfBirth DATETIME;

• Deleting a field:
ALTER TABLE Persons
28 DROP COLUMN DateOfBirth;
Altering a table example
• Create a table for movies, then modify a field after
creating the table

Movies Genre

Length Date of
release
Title

29 Week 4 Example 4
Constraints
• SQL allows us to put several constraints on the
values in our tables:

• By default, non-primary key fields can have NULL


(blank) values. We can disallow this with NOT NULL:

CREATE TABLE IF NOT EXISTS Pets ( A null value for “name”


will not be allowed
PetID INT AUTO_INCREMENT,
Name VARCHAR(100) NOT NULL,
PRIMARY KEY (PetID)
30 );
Constraints
• By default, non-primary key fields can have repeated
values. We can disallow this with UNIQUE:

CREATE TABLE IF NOT EXISTS Courses (


CourseID INT AUTO_INCREMENT,
Name VARCHAR(100) NOT NULL UNIQUE,
PRIMARY KEY (CourseID) Each course must be
); uniquely named

31
Constraints
• A default constraint sets an initial value for each new
record. However, the default can always be overridden

CREATE TABLE IF NOT EXISTS Jobs (


JobID INT AUTO_INCREMENT,
Title VARCHAR(500),
Salary DECIMAL DEFAULT 50000.0,
PRIMARY KEY (JobID) Default salary for
any new job position
);
32
Constraints example
• Create a table for Jobs with non-null job title. Use
“operations” as the default department

Department

Title Jobs Job ID

Salary

33 Week 4 Example 5

You might also like