SQL For Beginners
SQL For Beginners
By Anne-Marie Wright
The two tables are linked by the AddressId in a one to many relationship.
This means that there can be many Names linked to one address.
Name Table
Address Table
SELECT
SYNTAX: SELECT [{tableName}.]{fieldname}[,[{tablename}.] {fieldname}] FROM
{tablename}
SQL: to return all the fields and records in the name table
SELECT * FROM Name;
Result: NameID Surname FirstName MiddleName Male AddressId
1 Smith Andrew John true 1
2 Smithe Fred John true 2
3 Wright Anne false 3
4 Jones Emily Anne false 1
5 Wright David Peter true 3
Or we could only return certain fields
SQL: to return the id, surname, firstname fields of all the records of the name table
WHERE
To enable us to have a subset of the data we can add a Where clause to the end of the statement.
SQL: to return all the fields, but only the records that contain Smith in the Surname field
If we want to do a search.
SQL: to return all the fields, but only the records that start with 'An' in the Firstname table
There is no limited to the number of fields we can add to the WHERE clause
SQL: to return all fields from the records that have the Surname 'Wright AND that are Male
SQL: to return all the records that have the Surname 'Wright' OR that are Male
ORDER BY
It is possible to have the records returned in a certain order
o Ascending order is A to Z, 0 to 9
o Descending order is Z to A, 9 to 0
SQL: to return the Surname and Firstname from all the records sorted in ascending order by the
Firstname
SQL: to return the Surname and Firstname from all the records sorted in descending order by the
Firstname
GROUP BY
It is also possible to Group identical information together, but you have to put the fields that you want
returned. It is not possible to put a * to say the whole table as we have been doing in the previous
examples.
SQL: to return all the Male records and grouping the Middlename fields together, then the Surname and
finally the FirstName
What if we want to select the address for the names. For that we need to use a JOIN (The way tables are
joined together in a SQL statement depends on the database so I will give you 2 types Access and Oracle)
Access SQL:
Oracle SQL:
Access SQL:
Oracle SQL:
Modifying records
It's all very well being able to select the records but now we are looking at how to modify them.
The select statements are not going to return any errors if the sql is correct, they might return nothing,
but they will work. The queries that modify records can return errors. You must make sure that all the
fields that must have something in them are populated and that the fields have the correct type of data
(no letters in number fields etc). Otherwise it will not be able to save the record and will return an error.
Another hick-up might be if you had a relationship between two or more tables, you may find that you
cannot add data to one table before having a corresponding record in another table (i.e We have to have
an address in the address table before we can create a record in the Name table to link to it). This could
cause problems with deleting a record as well. There might be records in another table that are joined to
the record you are trying to delete. This again will cause an error and stop the process.
UPDATE
SYNTAX: UPDATE {tablename} SET [{tablename}.]{fieldname}=newvalue WHERE {criteria}
SQL:
SQL:
INSERT
OK so now we need to add new records to the table. For this we use the INSERT command
So to add a recordSQL:
To add records to a table from another table assume we had another table called OtherNames:
OtherNames
SQL:
Notice the select statement it is wirtten in just the same way as if it was a SQL query on its own. So we
could have had a subset of OtherNames added to the Name table.
i.e SQL:
DELETE
Deleting a record This is acheived by using the DELETE command
So if we wanted a table with just the women in we could use the following:
SQL:
This I hope has given you a simple idea of how SQL works. There is a lot more too it, but knowing this
should allow you to create small database applications.