SQL_Notes(AS_Level)
SQL_Notes(AS_Level)
SQL notes
Lucas Erkana
ST Georges
5/5/2020
Table of Contents
......................................................................................................................................................... 3
................................................................................................................................................................ 3
............................................................................................................................................................... 4
............................................................................................................................... 5
4.1 CREATE TABLE STUDENTS ........................................................................................................................................ 5
Student_table ............................................................................................................................................................ 5
4.2 CREATE TABLE Learners ........................................................................................................................................... 5
Learners_table ........................................................................................................................................................... 5
4.3 CREATE TABLE Items_table ...................................................................................................................................... 6
4.4 DELETE TABLE Students_table ................................................................................................................................. 6
4.4 DELETE TABLE Items_table ...................................................................................................................................... 6
4.5 ALTER TABLE Items_table ........................................................................................................................................ 6
............................................................................................................... 6
5.1 Add a field(Column) to Learners_table.................................................................................................................... 6
5.2 Add a field(Column) to Student_table ..................................................................................................................... 6
5.3 Add a field(Column) to Items_table......................................................................................................................... 6
5.4 Remove a field(Column) to Student_table .............................................................................................................. 7
5.5 Remove field(Column) to Learners_table ................................................................................................................ 7
5.6 Add a field(Column) to Student_table ..................................................................................................................... 7
.................................................................................................................................... 7
............................................................................................................................................................... 7
Student_table ............................................................................................................................................................ 7
Learners_table ........................................................................................................................................................... 8
Items_table ................................................................................................................................................................ 8
............................................................................................................................................................... 8
Student_table (BEFORE UPDATE) .............................................................................................................................. 8
Student_table (AFTER UPDATE) ................................................................................................................................. 8
Learners_table(BEFORE UPDATE) .............................................................................................................................. 9
Learners_table(AFTER UPDATE) ................................................................................................................................ 9
................................................................................................................................................................. 9
BEFORE DELETION ......................................................................................................................................................... 9
Items_table ................................................................................................................................................................ 9
AFTER DELETION ............................................................................................................................................................ 9
Items_table ................................................................................................................................................................ 9
1|Page
7.Commands for Retrieving Data ....................................................................................................................................10
7.1 Get all data from a table ........................................................................................................................................10
7.2 Get specific a record ..............................................................................................................................................10
7.3 Get only field data for a specific record.................................................................................................................10
7.4 Sorting(ORDER BY) .................................................................................................................................................10
8.Commands for Relationships ........................................................................................................................................11
8.1 Adding a Primary Key .............................................................................................................................................11
8.2 Remove a Primary Key ...........................................................................................................................................11
8.3 Adding a Foreign Key .............................................................................................................................................12
8.4 Joins .......................................................................................................................................................................13
2|Page
A database can have millions of records within it. There must be a way to interrogate a
database to extract information from it and also an efficient way to add or change the data.
The answer was the development of a standard database language called 'Structured Query
Language' or SQL.
A database contains one or more data structures called 'tables'. A table is made up of one or
more columns called 'fields' and a number of rows. Each row is a 'record'.
3|Page
A statement or command in SQL is called a query. Queries are formed from the built in
commands in SQL.
4|Page
If the table did not exist in the first place we could use the CREATE command
DATA TYPES:
Learners_table
5|Page
4.3 CREATE TABLE Items_table
Example:
6|Page
5.4 Remove a field(Column) to Student_table
Subject_Table Teacher_Table
INSERT INTO ‘student_table’ (‘ID’, Surname, ‘Age’, ‘Name’) VALUES (‘103’,’Singh’, ‘20’,’Sammy’)
Student_table
7|Page
INSERT INTO ‘Learners_table’ (‘StudentID’, Lastname, ‘DateofBirth’, ‘Grade’,’Firstname’) VALUES (‘36’,’Hangula’,
‘17/04/2005’,’9’,’John’)
Learners_table
Items_table
8|Page
Learners_table(BEFORE UPDATE)
Learners_table(AFTER UPDATE)
Example:
BEFORE DELETION
Items_table
9|Page
133020202020 Macaroni 500g 01/01/2021’ 25.00 30 10
ID Firstname lastname
23 John Shikongo
45 Anna Zack
26 Hank Endjala
87 Kenny Haufiku
ID Firstname lastname
26 Hank Endjala
87 Kenny Haufiku
10 | P a g e
23 John Shikongo
45 Anna Zack
ID Firstname lastname
45 Anna Zack
23 John Shikongo
87 Kenny Haufiku
26 Hank Endjala
Sort all the data in the table in ascending order according to the country:
SELECT * FROM Customers
ORDER BY Country;
11 | P a g e
8.3 Adding a Foreign Key
Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column
in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in the table it points
to.
12 | P a g e
8.4 Joins
Show all
SELECT Parent_Table.ParentField1, Parent_Table.parent_Field2,Child_table.Childfield1,Child_table.Childfield2,
Parent_table.ParentPrimaryKey, Parent_table.ParentForeignKey , Child_table.ChildPrimaryKey
FROM Child_Table
INNER JOIN Parent_Table ON Child_Table.Child_Primary_Key=Parent_table.ForeignKey;
13 | P a g e
14 | P a g e