SQL

Structured Query Language

SQL is designed to maniplulate databases. By this I mean search, add and update data. As well as changing the structure of the database. I will not go into too much detail in this tutorial, because even though SQL complies in general to a standard, some proprietry database vendors have made there own implementations of particular SQL functionality.
Therefore I will simply give a overview of the main SQL statements.

Select Statement

The select statement as it name implies selects data from a table or group of tables.
SELECT * FROM People
Will select all columns the asterix (*) indicates all columns should be selected but also because there is no WHERE clause the statement will also select all rows from the People table.
To restrict the amount of data returned something similar to the following can be done.
SELECT * FROM People WHERE name = 'tom'
The where clause indicates that only people with the name "tom" should be returned.
You can of course do selects using integers
SELECT * FROM People WHERE age >= 23
This selects all people with a age equal to or greater than 23.
You can also use logical operators such as AND or OR.
SELECT * FROM People WHERE name like 'mat%' AND age >= 23
This will return people whose age is equal to or greater than 23, the LIKE operator is used to do a partial match so in this case it will return any names that match the first part of the string and then anything after the wild card. So names like matt, mathew would all be returned.
Of course if you used a operator such as OR instead of AND it would return rows were the name or the age matched.

Update Statement

The update statement will update existing rows.
UPDATE People SET name = 'Rob'
The above statement will set all names in the table People to be Rob.
Like the Select statement a WHERE clause can be used to restrict what the statement is applied to.
UPDATE People SET name = 'Rob' WHERE id = 1
This statement will only update the People row with an id of 1.

Insert Statement

This statement inserts data into a pre-existing table.
INSERT INTO People (name, age) VALUES ('Dom', 25)
The above statement inserts Dom into the name column and 25 into the age column. The values in the first set of brackets are the columns to insert into, and the values in the bracket after the values keyword are the values to put into the columns specified in the first set of brackets in the exact order.

Delete Statement

This statement works in a similar way to the other statements in that it can also have a WHERE clause.
DELETE FROM People
This statement will delete all rows from the People table.
DELETE FROM People WHERE name = 'Rob'
This statement will delete rows where the People name is Rob.

No comments:

Post a Comment