SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management...

Preview:

Citation preview

SQLJohn Nowobilski

What is SQL?Structured Query Language

Manages Data in Database Management Systems based on the Relational Model

Developed in 1970s by IBM

Relational DatabaseMatches data by common

characteristics within data set

Based on relational algebra and relational calculus

Also known as a schema

RDBMSRelational Database Management

System

DBMS where data and relationships among data are stored in tables

Data can be accessed without changing forms of tables

Advanced Clothing Solutions

QueriesWay to access information from the

Database

All data and relationships are stored in tables

SQL: “SELECT - FROM - WHERE” statement format to access the relations/data stored in schema

TablesName of the table is the object you

are trying to represent

Consist of a tuple of columns that contain information about that table

OOP: member variables ≈ columns

Column DatatypesEach column has associated datatype

Set of “primitive” types as in OOP

INT, CHAR, VARCHAR, BOOL, FLOAT

DATE, TIME, YEAR, BLOB

SELECT - FROM“SELECT” clause accesses columns from

a selected table

Access all columns, or specify which you want to access

“FROM” clause determines which tables to access columns from

SELECT * FROM articles;

SELECT stock, rating, price FROM inventory;

WHERE clauseAdds constraints to returned results from

queries

Allows for relationships to be accessed between multiple tables

“AND” clause adds more constraints to returned results

SELECT * FROM articlesWHERE size =‘M’ AND gender =‘F’;

SELECT price, type FROM articles, inventoryWHERE articles.id = inventory.articleID;

DISTINCT clauseAllows for distinct results from

columns to be returned

Avoids redundancy in results

SELECT DISTINCT descriptionFROM articlesWHERE gender = ‘M’ AND type = ‘shirt’;

CREATE TABLECreates table in the database

Specify column names in CREATE TABLE statement

NOT NULL ensures column isn’t null when entries are inserted

CREATE TABLE articles(‘id’ INT NOT NULL,‘company_id’ INT NOT NULL,‘type’ VARCHAR(45),‘description’ VARCHAR(160),‘size’ VARCHAR(5),‘color’ VARCHAR(30),‘gender’ VARCHAR(1));

INSERTAdds entry into a table in the

database

Can populate as many or as few columns as you want, as long as they’re not required for insertion

INSERT INTOarticles(gender, type, color)VALUES(‘M’, ‘shirt’, ‘red’);

UPDATEUpdates column values in a table

Can update certain entries, or all entries in the table at once

UPDATE articles SET size=‘S’, color=‘blue’WHEREType=‘shirt’;

DELETERemoves entries from a table

Similar to “SELECT-FROM-WHERE” format

Use “DROP” to delete tables

DELETE FROM articlesWHERE size=‘S’ AND color=‘blue’;

DROP TABLE articles;

Thank youSources:

http://en.wikipedia.org/wiki/SQL

http://en.wikipedia.org/wiki/Relational_database_management_system

http://www.w3schools.com/sql/sql_syntax.asp

Recommended