INF311
Lesson 19
Introduction to SQL
SQL Basics
SQL : (Structured Query Language)
Is a common language to all database systems that enables
the programmer to write statements to alter the database
structure or the data inside the tables.
Querying Data:
Using SQL enables the programmer to retrieve and edit data
in database tables. The following operations are sufficient to
manipulate data:
- Insert
- Update
- Delete
- Select
SQL Basics
1- Insert:
Is used to add a record to a database table.
Syntax: Insert Into TableName ([Fields]) Values(FieldValues)
where TableName is the name of the table where the record
is to be added, Fields is the list of the field names (Between
Brackets and Comma Separated) to be inserted in the
record, FieldValues is the list of values (Comma separated)
corresponding to the field names specified before.
Ex:
Insert Into Students ([Name],[Age]) Values (‘Elie’, 23)
(The String Delimiter in SQL is the single quote ‘ )
SQL Basics
2- Update:
Is used to change the value of a certain field for a certain
number of rows in a table.
Syntax:
Update TableName Set FieldName = Value [Where Criteria]
where TableName is the name of the table that contains the
field to be updated, FieldName is the name of the field to be
updated, Value is the new value for the field, the Criteria (if
specified) determines which records to be updated, only
records that match the Criteria are updated, if no criteria is
given all the records of the table will be updated.
Ex:
Update Students Set Age = 13 ‘All students will be changed
Update Students Set Name = ‘Tony’ Where ID=‘STD0001’
SQL Basics
3- Delete:
Is used to delete records from a certain table
Syntax: Delete From TableName [Where Criteria]
where TableName is the name of the table from which
records will be deleted, Criteria determines which records will
be deleted (only records that match the criteria are deleted),
if no criteria is given, all the records in the table are deleted.
Ex: Delete From Students ‘ Deletes all records in the table
Delete From Students Where Age>25 And Probation>3
‘Deletes all records where the age is greater than 25 and the
‘Probation is greater than 3.
SQL Basics
4- Select:
Is used to select a certain number of records containing
selected fields that match the given criteria from database
tables.
Syntax:
Select FieldNames From TableNames [Where Criteria] [Order By SortFields]
Where FieldNames is the list of the fields to be retrieved, the
TableNames is the list of the tables containing the selected
fields, the Criteria determines which records to be retieved (If
given), the SortFields is the list of Selected field names that
determine the sort order of the records.
Ex: Select * From Students ‘Selects all the fields in the
‘students table, since no criteria is given, all records will be
‘returned.
Select ID,Name From Students Where Age>25 ‘Returns the
‘ID’s and names of all students who’s age is greater than 25
SQL Basics - Tips
* The Insert, Update and Delete Statements are called SQL
commands because they will commit changes to the database
without returning any values.
* The Criteria parameter in all SQL statements is usually a
comparison or a combination of comparisons between a field
and a value or 2 fields.
* Logical conjunctions can be used to combine multiple
comparisons (And, Or)
* If a Select statement involves more than 1 table, joins must
be made, to make a join an equality criteria between the
related fields in related tables must be combined with the
criteria parameter.
* If a selected field can be found in more than 1 selected table,
the source table must be specified before the fieldname
followed by a dot “.” (Students.ID)