sql1
DESCRIPTION
First presentation for Intro to SQLTRANSCRIPT
Shane Smith
Attributes (fields)◦ An attribute or field is a component of a record
that describes something about an item. Records
◦ A record is the representation of an individual item.
Table◦ A collection of records
Database◦ A collection of tables and rules for accessing the
tables
Originally developed by E.F. Codd in 1970 Organizes data into tables where each item
is a row and the attributes of the item are in columns.
Different from “flat file” databases because you can define “relationships” between items in different tables.
RDBMS stands for Relational DataBase Management System
RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables.
A table is a collections of related data entries and it consists of columns and rows.
Record
Attribute/Field
Tables
• Records become “rows”• Attributes/fields become “columns”• Rules determine the relationship between the tables and tie the data together to form 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 (in meters)◦ Birthday◦ When we added the entry
Binary◦ Database specific binary objects◦ Pictures, digital signatures, etc.
Boolean◦ True/False values
Character◦ Fixed width or variable size
Numeric◦ Integer, Real (floating decimal point), Money
Temporal◦ Time, Date, Timestamp
Name CharacterAddress CharacterCompany CharacterPhone Number CharacterURL/Web Page CharacterAge IntegerHeight Real (float)Birthday DateWhen we added the entry Timestamp
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 can’t have spaces or be “reserved words” like TABLE, CREATE, etc.
Name CharacterAddress CharacterCompany CharacterPhone Number CharacterURL/Web Page CharacterAge IntegerHeight Real (float)Birthday DateWhen we added the entry Timestamp
CREATE TABLE contacts (Name VARCHAR(40),Address VARCHAR(60),Company VARCHAR(60),Phone VARCHAR(11),URL VARCHAR(80),Age INT,Height FLOAT,Birthday DATE,WhenEntered TIMESTAMP
);Plan your tables very carefully!
Once created, they are difficult to change!
CREATE TABLE contacts (ContactID INT PRIMARY KEY,Name VARCHAR(40),Address VARCHAR(60),Company VARCHAR(60),Phone VARCHAR(11),URL VARCHAR(80),Age INT,Height FLOAT,Birthday DATE,WhenEntered TIMESTAMP
);If you are going to use the relational nature of a database,
don’t forget you need to have a unique way to access records!There is a way to make the key automatically increment,
so you don’t have to worry about which one is next.
Binary◦ Database specific binary objects
Boolean◦ True/False values (BOOLEAN)
Character◦ Fixed width (CHAR) or variable size (VARCHAR)
Numeric◦ Integer (INT), Real (FLOAT), Money (MONEY)
Temporal◦ Time (TIME), Date (DATE), Timestamp
(TIMESTAMP)
Generic Form
INSERT INTO tablename (column_name,…)VALUES (value,…)
INSERT INTO contacts (contactid,name,address,company,phone,url,age,height,birthday,whenentered)
VALUES(1,‘Joe’,’123 Any St.’,’ABC’,’800-555-1212’,‘http://abc.com’,30,1.9,’6/14/1972’,now());
INSERT INTO contacts (contactid,name,phone)VALUES (2,’Jane’,’212-555-1212’);
CREATE SEQUENCE contactidseq; Change the ContactID line in the
CREATE TABLE to:ContactID INT DEFAULT nextval(‘contactidseq’) PRIMARY KEY
Or when inserting into a tableINSERT contacts (contactid,name,phone)VALUES (nextval(‘contactidseq’),’Jack’,
‘716-555-1212’);
Generic FormSELECT column,… FROM table,…
WHERE condition GROUP BY group_by_expressionHAVING condition ORDER BY order_expression
The most used command Probably the most complicated also If used improperly, can cause very long
waits because complex computations
SELECT * FROM contacts;◦ Display all records in the ‘contacts’ table
SELECT contactid,name FROM contacts;◦ Display only the record number and names
SELECT DISTINCT url FROM contacts;◦ Display only one entry for every value of URL.
The WHERE “subclause” allows you to select records based on a condition.
SELECT * FROM contactsWHERE age<10;
◦ Display records from contacts where age<10 SELECT * FROM contacts
WHERE age BETWEEN 18 AND 35;◦ Display records where age is 18-35
The “LIKE” condition◦ Allows you to look at strings that are alike
SELECT * FROM contactsWHERE name LIKE ‘J%’;
◦ Display records where the name starts with ‘J’ SELECT * FROM contacts
WHERE url LIKE ‘%.com’;◦ Display records where url ends in “.com”
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
DROP TABLE tablename;
DROP TABLE contacts;