0% found this document useful (0 votes)
43 views

Databases: Jeffrey Miller, PH.D

The document provides an overview of databases and SQL. It discusses how database management systems store and organize data, and how they allow programmers to access, update, and analyze the stored data. It also introduces some common SQL statements like SELECT, INSERT, UPDATE, and DELETE that allow manipulating the data. Finally, it discusses how SQL scripts can be used to recreate databases and populate them with initial data.

Uploaded by

John Mathias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Databases: Jeffrey Miller, PH.D

The document provides an overview of databases and SQL. It discusses how database management systems store and organize data, and how they allow programmers to access, update, and analyze the stored data. It also introduces some common SQL statements like SELECT, INSERT, UPDATE, and DELETE that allow manipulating the data. Finally, it discusses how SQL scripts can be used to recreate databases and populate them with initial data.

Uploaded by

John Mathias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Databases

CSCI 201
Principles of Software Development

Jeffrey Miller, Ph.D.


[email protected]
Outline
• Databases
• SQL
• Try It!

USC CSCI 201L


Databases
▪ Database systems store data and provide a means of accessing,
updating, manipulating, and analyzing data
▪ Databases are stored in files on a file system, but they are arranged
in a manner that allows for fast queries and updates
▪ A Database Management System (DBMS) is designed for
programmers, not casual users

SQL-Based NoSQL
MySQL
MongoDB
PostgreSQL
Firebase
Oracle
Apache CouchDB
Microsoft SQL Server
Apache Cassandra
IBM DB2

USC CSCI 201L 3/14


Relational Databases
▪ Relational database management systems (RDBMS) provide three things
› Structure – the representation of the data
› Integrity – constraints on the data
› Language – means for accessing and manipulating data
OverallGrades
prefix num fname lname letterGrade
CSCI 103 Sheldon Cooper A
CSCI 104 Howard Wolowitz A-
CSCI 201 Leonard Hofstadter A
CSCI 201 Howard Wolowitz B
EE 101 Howard Wolowitz B-

Tables are called relations


Columns are called attributes
Rows are called tuples
Connections are called relationships

USC CSCI 201L 4/14


Relational Databases
OverallGrades
prefix num fname lname letterGrade
CSCI 103 Sheldon Cooper A
CSCI 104 Howard Wolowitz A-
Non-normalized
CSCI 201 Leonard Hofstadter A
CSCI 201 Howard Wolowitz B
EE 101 Howard Wolowitz B-

Class Student
classID prefix num
Normalized
studentID fname lname
1 CSCI 103 1 Sheldon Cooper
2 CSCI 104 2 Leonard Hofstadter
Grades
3 CSCI 201 3 Howard Wolowitz
gradeID classID studentID letterGrade
4 EE 101 4 Rajesh Koothrappali
1 1 1 A
2 2 3 A-
3 3 2 A
4 3 3 B
5 4 3 B-

USC CSCI 201L 5/14


Primary and Foreign Keys
▪ A primary key is a column (or a combination of multiple columns) in a table that
provides a unique reference to a row in the table
▪ A foreign key is a link between two tables that uniquely identifies a row in
another table Class Student
classID prefix num
studentID fname lname
1 CSCI 103
1 Sheldon Cooper
2 CSCI 104
2 Leonard Hofstadter
3 CSCI 201
3 Howard Wolowitz
Primary Keys 4 EE 101
4 Rajesh Koothrappali
5 EE 102
Class.classID
Student.studentID Grades
Grades.gradeID gradeID classID studentID letterGrade
Foreign Keys
1 1 1 A
Grades.classID
2 2 3 A-
Grades.studentID
3 3 2 A
4 3 3 B
5 5 3 B-

6/14
Outline
• Databases
• SQL
• Try It!
SQL
▪ The Structured Query Language (SQL) is the primary language
supported by DBMSs for accessing and manipulating data
▪ Some MySQL SQL statements you should know
› SHOW
› CREATE DATABASE
› USE
› CREATE TABLE
› INSERT
› UPDATE
› SELECT
› DELETE
› DROP
▪ If you are not familiar with SQL, there are many good tutorials
online and our textbook has a good chapter reference as well

USC CSCI 201L 8/14


SQL Scripts
▪ After installing a DBMS, you will be able to run SQL
scripts
▪ SQL scripts are files that contain a collection of SQL
statements that can be used for recreating databases and
populating them with initial or testing data
› This is often used in the testing phase of software engineering
to test different scenarios without requiring the QA engineers
to insert all of the data manually

USC CSCI 201L 9/14


Sample Script
1 DROP DATABASE IF EXISTS StudentGrades;
2 CREATE DATABASE StudentGrades;
3 USE StudentGrades;
4 CREATE TABLE Student (
5 studentID INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
6 fname VARCHAR(50) NOT NULL,
7 lname VARCHAR(50) NOT NULL
8 );
9 INSERT INTO Student (fname, lname) VALUES ('Sheldon', 'Cooper');
10 INSERT INTO Student (fname, lname) VALUES ('Leonard', 'Hofstadter');
11 CREATE TABLE Class (
Class Student
12 classID INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
classID prefix num studentID fname lname
13 prefix VARCHAR(5) NOT NULL,
14 num INT(4) NOT NULL 1 CSCI 103 1 Sheldon Cooper
15 ); 2 CSCI 104 2 Leonard Hofstadter
16 INSERT INTO Class (prefix, num) VALUES ('CSCI', 103);
17 INSERT INTO Class (prefix, num) VALUES ('CSCI', 104);
18 CREATE TABLE Grade ( Grade
19 gradeID INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, gradeID classID studentID letterGrade
20 classID INT(11) NOT NULL,
1 1 1 A
21 studentID INT(11) NOT NULL,
22 letterGrade VARCHAR(2) NOT NULL, 2 2 1 A
23 FOREIGN KEY fk1(classID) REFERENCES class(classID), 3 1 2 A
24 FOREIGN KEY fk2(studentID) REFERENCES student(studentID) 4 2 2 B
25 );
26 INSERT INTO Grade (studentID, classID, letterGrade) VALUES (1, 1, 'A');
27 INSERT INTO Grade (studentID, classID, letterGrade) VALUES (1, 2, 'A');
28 INSERT INTO Grade (studentID, classID, letterGrade) VALUES (2, 1, 'A');
29 INSERT INTO Grade (studentID, classID, letterGrade) VALUES (2, 2, ‘B');

USC CSCI 201L 10/14


SQL Visualization Tools
▪ A visualization tool connects to a database and allows
execution of SQL statements
› The tool then will graphically display the results
▪ Free MySQL Clients
› Command line client (Windows, Mac, Linux)
› MySQL Workbench (Windows, Mac, Linux)
› Sequel Pro (Mac)
› Toad for MySQL (Windows)

USC CSCI 201L 11/14


Executing SQL
▪ There are a few ways to execute SQL statements, which
typically depend on the DBMS
▪ We often want to be able to access databases from within
programs though
› This can be to insert, update, delete, or query the data
▪ The Java Database Connectivity (JDBC) drivers allow us to
embed SQL in our Java code and execute those statements on
a database programmatically

USC CSCI 201L 12/14


Outline
• Databases
• SQL
• Try it!

USC CSCI 201L


SQL
▪ Write SQL code to create the following database
Class Student
classID prefix num
studentID fname lname
1 CSCI 103
1 Sheldon Cooper
2 CSCI 104
2 Leonard Hofstadter
3 CSCI 201
3 Howard Wolowitz
4 EE 101
4 Rajesh Koothrappali
5 EE 102

Grades
gradeID classID studentID letterGrade
1 1 1 A
2 2 3 A-
3 3 2 A
4 3 3 B
5 5 3 B-

USC CSCI 201L 14/14

You might also like