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

8.3 Structured Query Language EDITED

The document provides an overview of SQL, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML) commands used in relational databases. It includes examples of SQL commands for creating and modifying tables, querying data, and using aggregate functions. Additionally, it covers the use of foreign keys and inner joins to manage relationships between tables.

Uploaded by

avinashnapaul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

8.3 Structured Query Language EDITED

The document provides an overview of SQL, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML) commands used in relational databases. It includes examples of SQL commands for creating and modifying tables, querying data, and using aggregate functions. Additionally, it covers the use of foreign keys and inner joins to manage relationships between tables.

Uploaded by

avinashnapaul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

A Level Computer Science P1 - Databases

Structured Query Language (SQL)


SQL – The standard query language used with relational databases for data definition and data modification.

Data Definition Language (DDL) and Data Manipulation Language (DML)

DDL – A language used to create, modify and remove the data structures that form a database.

DML – A language used to add, modify, delete and retrieve the data stored in a relational database.

DDL Commands

SQL (DDL) Command Description

in
CREATE DATABASE Creates a database
CREATE TABLE Creates a table definition
ALTER TABLE Changes the definition of a table
PRIMARY KEY Adds a primary key to a table
FOREIGN KEY … REFERENCES … Adds a foreign key to a table

st
Data Types for Attributes Description
CHARACTER
VARCHAR(n)
BOOLEAN
u Fixed length text
Variable length text
True or False; SQL uses the integers 1 and 0
g
INTEGER Whole number
REAL Number with decimal places
u
DATE A date usually formatted as YYYY-MM-DD
TIME A time usually formatted as HH:MM:SS
A

CREATE

 Create a table named Movies


r.

SQL:

CREATE Database VideoShop


M

CREATE TABLE Movies (


MovieID VARCHAR (6) ,
MovieTitle VARCHAR ,
MovieType VARCHAR ,
YearReleased INTEGER ,
DVD_In_Stock INTEGER );

Page 1 of 11
A Level Computer Science P1 - Databases

ALTER
 Altering table Movies and setting the primary key.

SQL:

ALTER TABLE Movies ADD PRIMARY KEY (MovieID);

Adding the primary key after the table has been created. This can also be done during the table creation.

 Adding new attributes/fields after creation of table.

in
SQL:

ALTER TABLE Movies ADD Price CURRENCY;

st
SQL:
u
g
ALTER TABLE Movies ADD Duration INTEGER;
u

Foreign Key - SQL


A

ALTER TABLE Suppliers ADD FOREIGN KEY (MovieID) REFERENCES Movies(MovieID);

DML Commands
r

SQL(DML) Query Command Description


SELECT FROM Fetches data from a database. Queries always begin with SELECT
M

WHERE Includes only rows in a query that match a given condition


ORDER BY Sorts the results from a query by a given column either alphabetically or
numerically
GROUP BY Arranges data into groups
INNER JOIN Combines rows from different tables if the join condition is true
SUM Returns the sum of all the values in the column
COUNT Counts the number of rows where the column is not NUL
AVG Returns the average value for a column with a numeric data type

SQL(DML) Maintenance Commands Description


INSERT INTO Adds new row(s) to a table
DELETE FROM Removes row(s) from a table
UPDATE Edits row(s) in a table

Page 2 of 11
A Level Computer Science P1 - Databases

SELECT
 To display all items from table Movies:

SQL:

SELECT * FROM Movies ;

in
 To display MovieID and MovieTitle released after year 2017 from table Movies:

st
SQL:

SELECT MovieID, MovieTitle


FROM Movies
WHERE YearReleased > 2017 ;
u
g
u
A

 To display MovieTitle of SciFi type from table Movies:


r.

SQL:
SELECT MovieTitle
FROM Movies
WHERE MovieType = 'SciFi' ;
M

Page 3 of 11
A Level Computer Science P1 - Databases

 To display all movies from table Movies

SQL:
SELECT MovieTitle
FROM Movies ;

in
 To display the MovieTitle and Price of all movies with a Duration of more than 90 minutes and of

st
MovieType Comedy.

SQL:
SELECT MovieTitle, Price
FROM Movies
WHERE Duration > 90 AND MovieType = 'Comedy';
u
g
u
A

 To display MovieTitle and Price of all movies with duration of less than 120 minutes or a price equal to $10.

SQL:
r.

SELECT MovieTitle, Price


FROM Movies
WHERE Duration < 120 OR Price = 10 ;
M

Page 4 of 11
A Level Computer Science P1 - Databases

ORDER BY
 To arrange (sort)records in specified order

SQL:
SELECT MovieID, MovieTitle
FROM Movies
ORDER BY MovieTitle ;

in
st
SQL:
SELECT MovieID, MovieTitle
FROM Movies
ORDER BY MovieTitle DESC ; u
g
u
A
r.

SQL:
SELECT MovieID, MovieTitle, YearReleased
M

FROM Movies
ORDER BY YearReleased ASC ;

Page 5 of 11
A Level Computer Science P1 - Databases

INSERT
 To add a new record into the table Movie

SQL:
INSERT INTO Movies
VALUES ('SF188', 'Men in Black 4', 'SciFi', 2019, 50, 30, 90);

in
 To add values for specific fields only.

SQL:
INSERT INTO Movies ( MovieId, MovieTitle, YearReleased, Duration )

st
VALUES ('SF213', 'Wonder Woman', 2017, 90);

u
g
u

DELETE
A

 To delete a record with a specific condition.

SQL:
DELETE FROM Movies
r.

WHERE MovieTitle = 'Wonder Woman';


M

Page 6 of 11
A Level Computer Science P1 - Databases

 Updating the field Duration with value 130

SQL:
UPDATE Movies
SET Duration = 130
WHERE MovieID = 'SF100';

GROUP BY
This is used with the SELECT statement to arrange identical data in groups. It is often used with aggregate

in
functions (COUNT, SUM, AVG…)

st
 To list the number of movies (according to their type) for each duration:

SQL:
SELECT COUNT(MovieType), Duration
FROM Movies
u
g
GROUP BY Duration;
u
A
r.

 To list the number of movies (according to their duration) for each type of movie:
M

SQL:
SELECT COUNT(Duration), MovieType
FROM Movies
GROUP BY MovieType;

Page 7 of 11
A Level Computer Science P1 - Databases

INNER JOIN
This statement retrieves data from two tables at least.

It returns rows that have matching values in both tables.

…………………………………………………………………………………………………………………………………………………………………………………………….

 Creating table Suppliers

CREATE TABLE Suppliers (


SupplierID INTEGER PRIMARY KEY ,
SupplierName VARCHAR (20),
SupplierAddress VARCHAR,
SupplierPhone INTEGER );

in
 Some records in table Suppliers

st
u
g
…………………………………………………………………………………………………………………………………………………………………………………………….

SQL:
u

SELECT Movies.MovieTitle, Suppliers.SupplierName


FROM Movies, Suppliers
WHERE Movies.MovieID = Suppliers.MovieID
A

AND Movies.Duration = 90;


r.
M

The query works as follows:


 The Movies table is searched for instances where the Duration is 90.
 For each instance the MovieID is noted.
 Then there is a search of the Suppliers table to find the examples of tuples having this value for MovieID.
 For each one found the MovieTitle and SupplierName are presented in the output.

Some versions of SQL require the explicit use of INNER JOIN

Page 8 of 11
A Level Computer Science P1 - Databases

SQL:
SELECT Movies.MovieType, Movies.MovieTitle, Suppliers.SupplierName,
Suppliers.SupplierAddress
FROM Movies INNER JOIN Suppliers
WHERE Movies.MovieID = Suppliers.MovieID
AND Movies.YearReleased > 2015;

NOTE:
1.
# is used to enclose a date value.

in
SELECT StaffID
FROM STAFF
WHERE CourseTitle = 'Excel Stage 1 ' AND CourseDate = #13/05/2022# ;

st
2.
The INNER JOIN keywords select all tuples from both tables where there is a match between the
columns in both tables.
u
The table name is added in front of the attribute name, using the 'dot notation'.
g
u
3.
Query Functions
The following three functions are available to use in a query. They all take an attribute name as
A

parameter. The function acts on several tuples returning a single value.

The following are examples of theitr use and so are called aggregate functions.
r.

i. Count and display the number of members of staff


SELECT COUNT (*)
FROM STAFF;
M

ii. Calculate and show the average number of years of service for all the members of staff.
SELECT AVG (YearsService)
FROM STAFF ;

iii. Calculate the total number of hours training done by the staff member with ID = 89.
SELECT SUM (Duration)
FROM STAFF
WHERE StaffID = 89 ;

Page 9 of 11
4. Adding a foreign key to a table

A foreign key can be stated when the table is first created:

For example: Setting a foreign key (MovieID) in the SUPPLIER table

FOREIGN KEY (MovieID) REFERENCES MOVIES (MovieID);

In brief:
When creating tables,
• Each field definition must be separated (from the next) by a comma.

in
• Statements can be written on a single line but they are much easier to comprehend when split across several lines.
• When using multiple lines, you must not forget to specify the parentheses (brackets) and commas.
• Usually, the first line includes the opening parenthesis and the last line will include the matching closing
parenthesis and semicolon.

st
• Whilst SQL is not case sensitive, keywords such as SELECT and FROM are written in upper case. This is good
u
practice as it helps to make your SQL statements more readable.
• It is a requirement that a semicolon (;) is placed at the end of the final line of the query to complete the SQL
statement before executing it.
g
u
A
r.
M

Page 10 of 11
Other sample SQL for INNER JOIN
SELECT Movies.MovieType, Movies.MovieTitle, Suppliers.SupplierName,
Suppliers.SupplierAddress
FROM Movies
INNER JOIN Suppliers
ON Movies.MovieID = Suppliers.MovieID ;

SELECT Movies.MovieType, Movies.MovieTitle, Suppliers.SupplierName,


Suppliers.SupplierAddress
FROM Movies , Suppliers
WHERE Movies.MovieID = Suppliers.MovieID;

SELECT Movies.MovieType, Movies.MovieTitle, Suppliers.SupplierName,


Suppliers.SupplierAddress
FROM Movies
INNER JOIN Suppliers
ON Movies.MovieID = Suppliers.MovieID
WHERE Movies.YearReleased > 2015;

Page 11 of 11

You might also like