Professional Documents
Culture Documents
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.
•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