sql1

26
Shane Smith

Upload: 5h4m4n

Post on 22-May-2015

410 views

Category:

Technology


0 download

DESCRIPTION

First presentation for Intro to SQL

TRANSCRIPT

Page 1: Sql1

Shane Smith

Page 2: Sql1
Page 3: Sql1

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

Page 4: Sql1

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.

Page 5: Sql1

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.

Page 6: Sql1

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

Page 7: Sql1

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

Page 8: Sql1

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

Page 9: Sql1

Name CharacterAddress CharacterCompany CharacterPhone Number CharacterURL/Web Page CharacterAge IntegerHeight Real (float)Birthday DateWhen we added the entry Timestamp

Page 10: Sql1

Creating tables with CREATE Adding data with INSERT Viewing data with SELECT Removing data with DELETE Modifying data with UPDATE Destroying tables with DROP

Page 11: Sql1

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.

Page 12: Sql1

Name CharacterAddress CharacterCompany CharacterPhone Number CharacterURL/Web Page CharacterAge IntegerHeight Real (float)Birthday DateWhen we added the entry Timestamp

Page 13: Sql1

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!

Page 14: Sql1

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.

Page 15: Sql1

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)

Page 16: Sql1

Generic Form

INSERT INTO tablename (column_name,…)VALUES (value,…)

Page 17: Sql1

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());

Page 18: Sql1

INSERT INTO contacts (contactid,name,phone)VALUES (2,’Jane’,’212-555-1212’);

Page 19: Sql1

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’);

Page 20: Sql1

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

Page 21: Sql1

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.

Page 22: Sql1

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

Page 23: Sql1

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”

Page 24: Sql1

Generic Form

DELETE FROM table WHERE condition;

DELETE FROM contacts WHERE age<13;

Page 25: Sql1

Generic Form

UPDATE table SET column=expression WHERE condition;

UPDATE contacts SET company=‘AOL’WHERE company=‘Time Warner’;

Page 26: Sql1

Generic Form

DROP TABLE tablename;

DROP TABLE contacts;