What Is SQL?: SQL Is A Standard Language For Accessing and Manipulating Databases
What Is SQL?: SQL Is A Standard Language For Accessing and Manipulating Databases
What Is SQL?: SQL Is A Standard Language For Accessing and Manipulating Databases
What is SQL?
SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard
An RDBMS database program (i.e. MS Access, SQL Server, MySQL) A server-side scripting language, like PHP or ASP SQL HTML / CSS
RDBMS
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems such as 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 collection of related data entries and it consists of columns and rows.
Database Tables
A database most often contains 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 "Persons": P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger
The table above contains three records (one for each person) and five columns (P_Id, LastName, FirstName, Address, and City).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements. The following SQL statement will select all the records in the "Persons" table:
Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server. We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL statement, but some database programs force you to use it.
SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database
The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are:
CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index
This chapter will explain the SELECT and the SELECT * statements.
P_Id 1 2 3
Now we want to select the content of the columns named "LastName" and "FirstName" from the table above. We use the following SELECT statement:
P_Id 1 2 3
SELECT * Example
Now we want to select all the columns from the "Persons" table. We use the following SELECT statement:
Navigation in a Result-set
P_Id 1 2 3
Most database software systems allow navigation in the result-set with programming functions, like: Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, etc. Programming functions like these are not a part of this tutorial. To learn about accessing data with function calls, please visit our ADO tutorial or our PHP tutorial.
City
Now we want to select only the distinct values from the column named "City" from the table above. We use the following SELECT statement:
Sandnes Stavanger
Now we want to select only the persons living in the city "Sandnes" from the table above. We use the following SELECT statement:
This is correct: SELECT * FROM Persons WHERE FirstName='Tove' This is wrong: SELECT * FROM Persons WHERE FirstName=Tove
For numeric values:
This is correct: SELECT * FROM Persons WHERE Year=1965 This is wrong: SELECT * FROM Persons WHERE Year='1965'
BETWEEN LIKE IN
Between an inclusive range Search for a pattern To specify multiple possible values for a column
The AND & OR operators are used to filter records based on more than one condition.
Now we want to select only the persons with the first name equal to "Tove" AND the last name equal to "Svendson": We use the following SELECT statement:
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola": We use the following SELECT statement:
ORDER BY Example
The "Persons" table: P_Id 1 2 3 4 LastName Hansen Svendson Pettersen Nilsen FirstName Ola Tove Kari Tom Address Timoteivn 10 Borgvn 23 Storgt 20 Vingvn 23 City Sandnes Sandnes Stavanger Stavanger
Now we want to select all the persons from the table above, however, we want to sort the persons by their last name. We use the following SELECT statement:
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
Now we want to insert a new row in the "Persons" table. We use the following SQL statement:
INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob')
The "Persons" table will now look like this: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 City Sandnes Sandnes Stavanger Stavanger
4 5
Nilsen Tjessem
Johan Jakob
Bakken 2
Stavanger
Now we want to update the person "Tjessem, Jakob" in the "Persons" table. We use the following SQL statement:
UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob'
The "Persons" table will now look like this: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 Nissestien 67 City Sandnes Sandnes Stavanger Stavanger Sandnes
3 4 5
Ppg http://w3schools.com/sql/sql_update.asp