SQL and You: A Friendly Introduction To Databases
SQL and You: A Friendly Introduction To Databases
What is a database?
Parts of a database
Attributes (fields)
Records
Table
Database
Parts of a database
Record Tables Attribute/Field
Attributes/fields become columns Records become rows Rules determine the relationship between the tables and tie the data together to form a database
Many people ask for new databases when in fact they only need a new table within an existing database. The data within the tables should be all related somehow.
By owner By project
Creating a database
What information are we trying to store? How do we describe the information? Phone Book/Contact entries Name Address Company Phone Number URL/Web Page Age Height Birthday When we added the entry
Data Types
Binary
Boolean
Character
Numeric
Temporal
E. F. Codd in 1972 wrote a paper on Further Normalization of the Data Base Relational Model Normal forms reduce the amount of redundancy and inconsistent dependency within databases. Codd proposed three normal forms and through the years two more have been added.
No rules have been applied Where most people start (and stop) No room for growth Usually wastes space
Contacts
Name Company Address Phone1 Phone2 Phone3 ZipCode
Joe
ABC
123
5532
2234
3211
12345
Jane
Chris
XYZ
PDQ
456
789
3421
2341 3211
14454
14423
Eliminate repeating columns in each table Create a separate table for each set of related data Identify each set of related data with a primary key
Contacts
Id Name Company Address Phone ZipCode
1
1 1 2 3 3
Joe
Joe Joe Jane Chris Chris
ABC
ABC ABC XYZ PDQ PDQ
123
123 123 456 789 789
5532
2234 3211 3421 2341 3211
12345
12345 12345 14454 14423 14423
Benefits: Now we can have infinite phone numbers or company addresses for each contact. Drawback: Now we have to type in everything over and over again. This leads to inconsistency, redundancy and wasting space. Thus, the second normal form
Create separate tables for sets of values that apply to multiple records Relate these tables with a foreign key.
People
Id 1 2 Name Joe Jane Company ABC XYZ Address 123 456 Zip 12345 14454
Chris
PDQ
789
14423
PhoneNumbers
PhoneID 1 2 Id 1 1 Phone 5532 2234
3
4 5 6
1
2 3 3
3211
3421 2341 3211
People
Id 1 2 3 Name Joe Jane Chris Addressid 1 2 3
PhoneID 1 2 3 4 5 6
Id 1 1 1 2 3 3
Address
Company ABC XYZ PDQ Address 123 456 789 Zip 12345 14454 14423
Kinds of Relationships
One to One
One to Many
Many to Many
Artist to Album
One album, many artists Many artists, one album Many artists, many album
In a many to many relationship, independent entities cannot be stored in the same table. The same phone number 3211 shared by 1 and 3 can now be changed in one spot.
PhoneNumbers
PhoneID 1 2 3 4 5 Phone 5532 2234 3211 3421 2341
PhoneRelations
PhoneRelID 1 2 3 4 5 6 Id 1 1 1 2 3 3 PhoneID 1 2 3 4 5 3
The very esoteric one that is probably not required to get the most out of your database. The original table must be reconstructed from the tables into which it has been broken down. The rule ensures that you have not created any extraneous columns and all the tables are only as large as they need to be.
First Form Eliminate replicated data in tables Create separate tables for each set of related data Identify each set of related data with a primary key Second Form Create separate tables for sets of values that apply to multiple records Relate the tables with a foreign key Third Form Eliminate fields that do not depend on the primary key Fourth Form In many-to-many relationships, independent entities cannot be stored in the same table Fifth Form So rarely used but the purest form of separation of data
Keep normalization in mind. Dont replicate data in a table. If you break the rules, know why you are breaking the rules and do it for a good reason.
Creating tables with CREATE Adding data with INSERT Viewing data with SELECT Removing data with DELETE Modifying data with UPDATE Destroying tables with DROP
Generic form
CREATE TABLE tablename ( column_name data_type attributes, column_name data_type attributes, )
Table and column names cant have spaces or be reserved words like TABLE, CREATE, etc.
Data Types
Binary
Boolean
Character
Numeric
Temporal
Generic Form
INSERT INTO tablename (column_name,) VALUES (value,)
CREATE SEQUENCE contactidseq; Change the ContactID line in the CREATE TABLE to:
ContactID INT DEFAULT nextval(contactidseq) PRIMARY KEY
Generic Form SELECT column, FROM table, WHERE condition GROUP BY group_by_expression HAVING condition ORDER BY order_expression The most used command Probably the most complicated also If used improperly, can cause very long waits because complex combinations
The WHERE subclause allows you to select records based on a condition. SELECT * FROM contacts WHERE age<10;
Additional selections
Generic Form
DELETE FROM table WHERE condition; DELETE FROM contacts WHERE age<13;
Generic Form
UPDATE table SET column=expression WHERE condition; UPDATE contacts SET company=AOL WHERE company=Time Warner;
Generic Form
SELECT name,phone,zip FROM people, phonenumbers, address WHERE people.addressid=address.addressid AND people.id=phonenumbers.id;
People
Id Name Addressid
1
2 3
Joe
Jane Chris
1
2 3
PhoneNumbers
PhoneID 1 2 3 4 5 6 Id 1 1 1 2 3 3 Phone 5532 2234 3211 3421 2341 3211 AddressID 1 2
Address
Company ABC XYZ Address 123 456 Zip 12345 14454
PDQ
789
14423
Inner Join
Other versions
SELECT name,phone FROM people LEFT JOIN phonenumbers ON people.id=phonenumbers.id; SELECT name,phone FROM people RIGHT JOIN phonenumbers ON people.id=phonenumbers.id; SELECT name,phone FROM people FULL JOIN phonenumbers ON people.id=phonenumbers.id;
SELECT name,phone,zip FROM people, phonenumbers, address WHERE people.addressid=address.addressid AND people.id=phonenumbers.id;
SELECT name,phone,zip FROM people JOIN phonenumbers ON people.id=phonenumbers.id JOIN address ON people.addressid=address.addressid;
SELECT * FROM contacts WHERE name is NULL; SELECT * FROM contacts WHERE zip IN (14454,12345); SELECT * FROM contacts WHERE zip NOT IN ( SELECT zip FROM address WHERE state=NY );
GROUP BY/HAVING
The GROUP BY clause allows you to group results together with aggregate functions
GROUP BY Examples
SELECT * FROM contacts GROUP BY company; SELECT company,count(company) FROM contacts GROUP BY company; SELECT company,count(company) FROM contacts GROUP BY company HAVING count(company) > 5;
ORDER BY
The ORDER BY clause allows you to sort the results returned by SELECT.
SELECT * FROM contacts ORDER BY company; SELECT * FROM contacts ORDER BY company, name;
Views
You can use CREATE VIEW to create a virtual table from a SELECT statement.
CREATE VIEW contactview AS (SELECT name,phone,zip FROM people,phonenumbers,address WHERE people.id=phonenumbers.id AND people.addressid=address.addressid);