0% found this document useful (0 votes)
4 views

SQL_Notes(AS_Level)

This document contains comprehensive SQL notes detailing commands for creating, altering, and deleting tables, as well as retrieving and managing data within a database. It includes examples of SQL syntax for various operations such as inserting records, updating data, and establishing relationships between tables using primary and foreign keys. The notes serve as a guide for understanding and utilizing SQL effectively for database management.

Uploaded by

daizhuomingdai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_Notes(AS_Level)

This document contains comprehensive SQL notes detailing commands for creating, altering, and deleting tables, as well as retrieving and managing data within a database. It includes examples of SQL syntax for various operations such as inserting records, updating data, and establishing relationships between tables using primary and foreign keys. The notes serve as a guide for understanding and utilizing SQL effectively for database management.

Uploaded by

daizhuomingdai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

2020

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.

SQL contains a number of commands to manage data, these include

4|Page
If the table did not exist in the first place we could use the CREATE command

CREATE table_name (primary_field(field) data type(length), field data type(length))

DATA TYPES:

INT – Integer (example: -18, 17)

TEXT - string (example: “John”, “[email protected]”)

VARCHAR - String (example: “John”, “[email protected]”)

DATE – Time (example: 18/05/2020, 18 November 19)

TIME – Time (example: 17:05)

DOUBLE – Real number/Decimal numbers (example: 67.99, 14.99)

BIT – True or False, 1 or 0

BOOLEAN – Yes/No(True/False, Present/Not Present, On/Off)


4.1 CREATE TABLE STUDENTS
Student_table

CREATE Student_Table (PRIMARYKEY(ID) INT(11), Surname VARCHAR(30), OtherNames


VARCHAR(30), AGE INT(3))

ID Surname OtherNames Age

4.2 CREATE TABLE Learners

Learners_table

CREATE Learners_table (StudentID INT(11), LastName VARCHAR(30), DateofBirth TEXT,


Grade INT(3), Class VARCHAR(1))

StudentID LastName DateofBirth Grade Class

5|Page
4.3 CREATE TABLE Items_table

CREATE Items_Table (Barcode INT(11), Item_Name VARCHAR(30), ItemSize VARCHAR(30),


ExpiryDate DATE, price DOUBLE(6) ,Quantity INT(4))

Barcode Item_name ItemSize ExpiryDate Price Quantity

4.4 DELETE TABLE Students_table

DROP TABLE table_name

Example:

DROP TABLE Student_Table

4.4 DELETE TABLE Items_table

DROP TABLE Items_Table

4.5 ALTER TABLE Items_table

5.1 Add a field(Column) to Learners_table


ALTER TABLE Learners_table ADD firstname TEXT

5.2 Add a field(Column) to Student_table


ALTER TABLE Student_table ADD Name TEXT

5.3 Add a field(Column) to Items_table


ALTER TABLE Items_table ADD items_Left INT

6|Page
5.4 Remove a field(Column) to Student_table

ALTER TABLE Student_table DROP COLUMN Othernames


5.5 Remove field(Column) to Learners_table

ALTER TABLE Learners_table DROP COLUMN Class

5.6 Add a field(Column) to Student_table


ALTER TABLE Table_name
ADD FOREIGN KEY (foreign_Key) REFERENCES Foreign_Table(Primary_Key)

Subject_Table Teacher_Table

Subject_ID(PK) Subject Teacher_ID(FK)


Teacher_ID(PK) Teacher_Name Teacher_Surname
MAT Mathematics 1
1 John Henry
Geo Geography 1
2 James Erkana

ALTER TABLE Subject


ADD FOREIGN KEY (Teacher_ID) REFERENCES Teacher_Table(Teacher_ID);

INSERT INTO ‘student_table’ (‘ID’, Surname, ‘Age’, ‘Name’) VALUES (‘103’,’Singh’, ‘20’,’Sammy’)

Student_table

ID Surname Age Name

103 Singh 20 Sammy

7|Page
INSERT INTO ‘Learners_table’ (‘StudentID’, Lastname, ‘DateofBirth’, ‘Grade’,’Firstname’) VALUES (‘36’,’Hangula’,
‘17/04/2005’,’9’,’John’)

Learners_table

StudentID LastName DateofBirth Grade Firstname

36 Hangula 17/04/2005 9 John

INSERT INTO ‘Items_table’ (‘Barcode’, Item_name, ‘Item_name’, ‘ItemSize’, ’ExpiryDate’) VALUES


(‘100020202020’,’Fanta’, ’500ml ’, ‘17/12/2021’,’15.00’,’28’,’20’)

INSERT INTO ‘Items_table’ (‘Barcode’, Item_name, ‘Item_name’, ‘ItemSize’, ’ExpiryDate’) VALUES


(‘133020202020’,’Macoroni’, ’500g ’, ‘01/01/2021’,’25.00’,’30’,’10’)

Items_table

Barcode Item_name ItemSize ExpiryDate Price Quantity Items_left

100020202020 Fanta 500ml 17/12/2021 15.00 28 20

133020202020 Macoroni 500g 01/01/2021’ 25.00 30 10

Student_table (BEFORE UPDATE)

ID Surname Age Name

103 Singh 20 Sammy


UPDATE ‘student_table’ SET ‘Age’ = 15 WHERE ‘ID’ = 103

Student_table (AFTER UPDATE)

ID Surname Age Name

103 Singh 15 Sammy

8|Page
Learners_table(BEFORE UPDATE)

StudentID LastName DateofBirth Grade Firstname

36 Hangula 17/04/2005 9 John

UPDATE ‘Learners_table’ SET ‘Grade’=10 WHERE ‘StudentID’ = 36

Learners_table(AFTER UPDATE)

StudentID LastName DateofBirth Grade Firstname

36 Hangula 17/04/2005 9 John

Let us delete a record

DELETE FROM table_name WHERE primary_field=data

Example:

BEFORE DELETION
Items_table

Barcode Item_name ItemSize ExpiryDate Price Quantity Items_left

100020202020 Fanta 500ml 17/12/2021 15.00 28 20

133020202020 Macaroni 500g 01/01/2021’ 25.00 30 10

DELETE FROM Items_table WHERE Barcode=100020202020


AFTER DELETION
Items_table

Barcode Item_name ItemSize ExpiryDate Price Quantity Items_left

9|Page
133020202020 Macaroni 500g 01/01/2021’ 25.00 30 10

7.Commands for Retrieving Data

7.1 Get all data from a table

SELECT primary_field, field2, field3, field4, field5, field6


FROM table_name

7.2 Get specific a record

SELECT primary_field, field2, field3, field4, field5, field6


FROM table_name
WHERE primary_field = 'data';

7.3 Get only field data for a specific record


SELECT field3, field4, field6
FROM table_name
WHERE primary_field = 'data';

7.4 Sorting(ORDER BY)


SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
MyGuests_table

ID Firstname lastname
23 John Shikongo
45 Anna Zack
26 Hank Endjala
87 Kenny Haufiku

SELECT id, firstname, lastname FROM MyGuests_table ORDER BY lastname ASC

ID Firstname lastname
26 Hank Endjala
87 Kenny Haufiku

10 | P a g e
23 John Shikongo
45 Anna Zack

SELECT id, firstname, lastname FROM MyGuests_table ORDER BY lastname DSC

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;

8.Commands for Relationships


8.1 Adding a Primary Key
ALTER TABLE Persons
ADD PRIMARY KEY (ID);

8.2 Remove a Primary Key


ALTER TABLE Persons
DROP PRIMARY KEY;

11 | P a g e
8.3 Adding a Foreign Key

Orders table will be the Parent Table

Persons table will be the Child Table

ALTER TABLE Parent_table


ADD FOREIGN KEY (Foreign_key) REFERENCES Childe_table(Primary_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.

ALTER TABLE Orders


ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

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;

Show specific record from Parent_table


SELECT Parent_Table.ParentField1, Parent_Table.parent_Field2, Child_table.Childfield1,Child_table.Childfield2
Parent_table.ParentPrimaryKey
FROM Child_Table
INNER JOIN Parent_Table ON Child_Table.Child_Primary_Key=Parent_table.ForeignKey;
WHERE Parent_Table.ParentPrimaryKey=1;

Show specific record from child_table


SELECT Parent_Table.ParentField1, Parent_Table.parent_Field2, Child_table.Childfield1,Child_table.Childfield2,
Child_table.ChildPrimaryKey
FROM Child_Table
INNER JOIN Parent_Table ON Child_Table.Child_Primary_Key=Parent_table.ForeignKey;
WHERE Child_Table.ChildPrimaryKey=1;

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderID


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

13 | P a g e
14 | P a g e

You might also like