You are on page 1of 49

Database Fundamentals

SQL
Structured Query Language
see-quel
SQL:
• Data Definition Language (DDL),
• Data Manipulation Language (DML), and
• Data Control Language (DCL)
SQL Query Structure
Understanding databases

A collection of data

•Focus on
•Relational Database Management System
(RDMS)
Tables
• In RDMS, tables store data in columns and rows.

• A NULL is a missing or unknown value. It is not 0.


Fields/Cells
• an intersection of a row and a column.
Records/rows
• row contains values in a horizontal division of data.
Columns
• column contains values in a vertical division of data.
Data Integrity
• the consistency and accuracy of the data.
• In RDMS, keys enforce data integrity.
• key is user-defined and forces values in a table to
conform to a specified standard.
Types of integrity
Entity integrity

•To ensure that each row in a table is


identifiably unique.
•Use
•Uniqueness constraint
•not null, and
•primary key constraints.
Unique constraints
• A type of Key used to avoid duplicate data.
• can apply to multiple columns
• unique constraint on one column => that column
cannot have duplicates
• Otherwise, the row will not allow insertion or
update
parkkey: a unique constraint
Composite unique constraint
• playerID, yearID, and teamID
Not null constraints
• used to avoid missing data
• create a not null constraint on a column, and the
column will not allow insertion or update when value
is null

• e.g. birthYear constraint should be not null


• deathYear constraint should allow nulls
primary key
• ensures that all values in a column are not null and unique.
• combines the unique and not null constraints
• You can only have one primary key per table.
• You can create a primary key on multiple columns: composite key.
playerID: primary key
playerID, yearID, and teamID :
composite primary key
Referential integrity
• the consistency and accuracy between tables that can be
linked together.
• set primary key on the parent table and a foreign key on the
child table
• a foreign key must reference a valid, existing primary key
• bad referential integrity creates orphaned records
E.g
• delete a player from the first table

• you didn't delete the corresponding record in the second table


• you create orphaned records
foreign key constraint
• If you set a foreign key constraint on the salary column, you can’t delete
the player from the parent table without first deleting the corresponding
salary rows in the salary table.
• foreign key constraint => prevent addition of data to child table and not
parent table or vice versa.
• Danger: No error is given when referential integrity is violated
• Consequences: Orphaned records that may never show up in reports or
query results
• the foreign key must reference a column (the primary key) in another
table.
• the foreign key can be any data type and accept duplicate and null values
foreign key constraint => maintains 3
types of table relationships

•One-to-one
•One-to-many
•Many-to-many
One-to-one
• one table has just one corresponding row in another table.

• E.g.
• A table with employees and computers.
• Each employee has one computer.
One-to-many
• when one table has none, one, or many corresponding rows in
another table.

• E.g.
• a tables with adults and children.
• An adult table row may have none, one, or many rows in the child
table.
Many-to-many
• when many rows in one table correspond to many rows in another
table.

• E.g.
• customers and products tables.
• Many Customers can purchase many products.
one-to-many relationship
Domain integrity
• To ensure that
• data follows defined rules for formatting, range, and value using
check and default constraints

• check constraint => ensure that all values in a column are within a
range of values.
• check constraint evaluates as either true or false.
E.g. A player can either be inducted into the
hall of fame or not.
check constraint can be applied
to multiple columns
• deathYear cannot be lower than birthyear
Default constraint
• To ensure all rows in a column have a value
• assigns a default value to a field.
• to avoid having a null value for a field if a user doesn't specify a value.

• E.g.
• goals_scored set to 0 as default
Basic SQL Querying: CREATE
SELECT
elimination of duplicates
• Almighty SELECT *
Multiple table queries
Renaming using “as” Clause
• Rename attribute name to instructor name

• Question: For all instructors in the university who have taught some
course, find their names and the course ID of all courses they taught.
Rename to compare tuples in the
same relation
• Question: Find the names of all instructors whose salary is greater
than at least one instructor in the Biology department
String Operators
• Question: Find the names of all departments whose building name
includes the substring 'Watson'.
use a backslash (∖) as the
escape character
order by
• lists items in ascending order
specify the order

You might also like