250 likes | 436 Views
SQL (Structured Query Language). CS 157A By JIA HUANG. Definition . SQL- is a database sublanguage for querying and modifying relational databases. History.
E N D
SQL (Structured Query Language) • CS 157A • By • JIA HUANG
Definition • SQL- is a database sublanguage for querying and modifying relational databases
History • SQL was developed by IBM research in the mid 70’s and standardized by ANSI in 1986.
Language Structure • SQL is a keyword based language. • Each statement begins with a unique keyword. • SQL statements consist of clauses which begin with a keyword. • SQL syntax is not case sensitive.
There are 3 basic categories of SQL Statements: • SQL-Data Statements • SQL-Transaction Statements • SQL-Schema Statements
SQL-Data Statements • SELECT -- query tables and views in the database • INSERT -- add rows to tables • UPDATE-- modify columns in table rows • DELETE -- remove rows from tables
SELECT column_name (using * for all column) FROM table_name SELECT column_name FROM table_name WHERE condition Syntax
Example “Customer" table SELECT LastName,FirstName FROM Customer
Example “Customer" table SELECT Address FROM Customer WHERE LASTNAME=“Hanson”
With the WHERE clause, the following operators can be used:
Syntax INSERT INTO table_name VALUES (value1, value2,....) You can also specify the columns for which you want to insert data: INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....)
Example “Customer" table INSERT INTO Customer VALUES (“Peterson”, ”Kari”, “43 Story”, ”Reno”)
Example “Customer" table INSERT INTO Customer (LastName, City) VALUES (“Peterson”, ”Reno”)
Syntax • UPDATE table_name SET column_name = new_valueWHERE column_name = some_value
Example “Customer" table UPDATE Customer SET Address = “21 Century” WHERE LastName =“Stevenson”
Syntax • DELETE FROM table_name WHERE column_name = some_value You can delete all rows by: DELETE FROM table_name DELETE * FROM table_name
Example “Customer" table DELETE FROM Customer WHERE LastName =“Stevenson”
Example “Customer" table DELETE FROM Customer OR DELETE * FROM Customer
SQL-Schema Statements • CREATE TABLE -- create tables • DROP TABLE -- drop tables • GRANT -- grant privileges on tables and views to other users • REVOKE-- revoke privileges on tables and views from other users
Syntax • To create a database: CREATE DATABASE database_name To create a table in a database: CREATE TABLE table_name( column_name1 data_type,column_name2 data_type, ....... )
Example • CREATE TABLE Customer ( LastName varchar, FirstName varchar, Address varchar, Age int ) Result: “Customer" table
Syntax • To delete a table (the table structure, attributes, and indexes will also be deleted): • To delete a database: DROP DATABASE database_name DROP TABLE table_name
Some other commands: ORDER BY AND & OR JOIN UNION ALTER GROUP BY SELECT INTO …
References • http://www.w3schools.com/sql/default.asp • http://www.firstsql.com/tutor.htm • http://www.tc.umn.edu/~hause011/