Professional Documents
Culture Documents
1
XAMPP
• There are many options for database development
environments. XAMPP is one of the most popular,
industry-standard options
3
XAMPP startup
Windows Mac
4
XAMPP startup
Windows Mac
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”
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
• 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
12
Creating tables through phpMyAdmin
• Choose a database and select “Structure.” Name the
table, select the number of columns and press “Go”:
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
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
20
Creating a table example
• Transform this ER diagram entity into a database
table:
Name
Supplier
Industry Suppliers
ID
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”)
23
Adding data to databases
• Data can be added in bulk from files, if available. In
phpMyAdmin, select “Import” and choose a file
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
• 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:
31
Constraints
• A default constraint sets an initial value for each new
record. However, the default can always be overridden
Department
Salary
33 Week 4 Example 5