SQL Basic 12-28-11
SQL Basic 12-28-11
databases.
What is SQL? -SQL stands for Structured Query Language -SQL allows you to access a database -SQL is an ANSI standard computer language -SQL can execute queries against a database -SQL can retrieve data from a database -SQL can insert new records in a database -SQL can delete records from a database -SQL can update records in a database -SQL is easy to learn SQL - Structured Query Language (Basic) SQL Database Tables: -A database most oftencontains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders" ). Tables contain records (rows) with data Below is an example of a table called "Employee". LastName XXXXX XXXXX FirstName XXXX XXXXX Address XXX XXXXX City
SQL Queries: -With SQL, we can query a database and have a result set returned. A query like this: SELECT LastName FROM Employee
Gives a result set like this: LastName Abdullah Abu Hamdeh Hannah Note: Some database systems require a semicolon at the end of the SQL statement. SQL Data Manipulation Language (DML) -SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. These query and update commands together form the Data Manipulation Language(DML) part of SQL: -SELECT - extracts data from a database table -UPDATE - updates data from a database table -DELETE - deletes data from a database table -INSERT INTO - inserts new data from a database table SQL Sata Definition Language (DDL) -The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are: -CREATE TABLE - creates new database table -ALTER TABLE - alters (changes) a database table -DROP TABLE - deletes a database table - CREATE INDEX - creates an index (search key) -DROP INDEX - deletes an index The SELECT Statement - The SELECT Statement is used to select data from a table -The tabular result is stored in a result table (called the result-set). Syntax
The SELECT DISTINCT Statement -The DISTINCT keyword is used to return only distinct (different) values. -The SELECT statement returns information from table columns. But what if we only want to select distinct elements? -With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement: Syntax SELECT DISTINCT column_name(s) FROM table_name
The WHERE Clause: -The WHERE clause is used to specify a selection criterion. -To conditionally select data from a table, a WHERE clause can be added to the SELECT statement. Syntax: SELECT column FROM table WHERE column operator value
With the WHERE clause, the following operators can be used: Operator = <> > < >= <= BETWEEN LIKE Description Equal Not equal Greater than Less than Greater than or equal Less than or equal Between an inclusive range Search for a pattern
The INSERT INTO Statement - The INSERT INTO statement is used to insert new rows into a table. Syntax:
You can also specify the columns for which you want to insert data: INSERT INTO table_name (column1, column2,.....) VALUE (value1, value2,.....)
The Update Statement: -The UPDATE statment is used to modify the data in a table. Syntax: UPDATE table_name SET column_name = new_value WHERE column_name = some_value
The Delete Statement: -The DELETE Statement is used to delete rows in a table. Syntax: DELETE FROM table_name WHERE column_name = some_value