SQL Curso
SQL Curso
What is SQL?
However, to be compliant with the ANSI standard, they all support at least the
major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in
a similar manner.
Most of the SQL database programs also have their own proprietary extensions in addition to the SQL
Using SQL in Your Web
Site
To build a web site that shows some data from a database, you will need the
following:
v
An RDBMS database program (i.e. MS Access, SQL
Server, MySQL)
v
A server-side scripting language, like PHP or ASP
v
SQL
v
HTML / CSS
RDBMS
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 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:
SELECT * FROM Persons
The query and update commands form the DML part of SQL:
to select the content of the columns named "LastName" and "FirstName" from the
want to select only the distinct values from the column named "City" from the table
HERE clause is used to extract only those records that fulfill a specified cri
Now we want to select only the persons living in the city "Sandnes" from the table above.
ant to select only the persons with the first name equal to "Tove" AND the last name equal to
elect only the persons with the last name equal to "Svendson" AND the first name equal to
If you want to sort the records in a descending order, you can use the DESC keyword.
to select all the persons from the table above, however, we want to sort the persons by the
The first form doesn't specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
INSERT INTO Example
We have the following "Persons" table:
UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
se in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit
SQL UPDATE Example
The "Persons" table:
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'
TE Persons
ddress='Nissestien 67', City='Sandnes'
DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value
syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the W
DELETE Example
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.
s in a table without deleting the table. This means that the table structure, attribute
Note: Be very careful when deleting records. You cannot undo this statement!