Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
sql
DDL & DML
What is DDL & DML?
-
Data Definition Language (DDL) is used to build and manage the structure of a relational database
-
It defines how data is stored by creating and modifying tables, fields, indexes, and relationships
-
Data Manipulation Language (DML) is used to manage the data stored within the database structures created by DDL
-
It deals with adding, updating, deleting, and retrieving data
-
Both DDL and DML use SQL syntax to interact with the database
-
DDL structures the database, while DML maintains the contents of the database
-
Most modern DBMSs use SQL to support both DDL and DML operations
Basic DDL commands
|
Command |
Purpose |
Example |
|---|---|---|
|
|
Creates a new database |
|
|
|
Creates a new table with specified fields and data types |
|
|
Data types for attributes: |
Defines the type of data each field can store |
|
|
|
Fixed-length text |
|
|
|
Variable-length text (max length n) |
|
|
|
True or False value |
|
|
|
Whole numbers |
|
|
|
Decimal numbers |
|
|
|
Calendar date |
|
|
|
Time of day |
|
|
|
Changes an existing table (e.g. add or remove fields) |
|
|
|
Sets a field as the primary key (unique identifier) |
|
|
|
Sets up a foreign key relationship between tables |
|
Example: School database
-
You are designing a database to store information about students and the courses they are enrolled in
-
Each student can enrol in many courses, and each course can have many students (a many-to-many relationship)
-
Three tables are needed:
-
Students– to store student details -
Courses– to store course details -
Enrolments– a link table to handle the many-to-many relationship
-
-- Create the Students table
CREATE TABLE Students ( StudentID INTEGER, FirstName VARCHAR(50), LastName VARCHAR(50), DateOfBirth DATE, PRIMARY KEY (StudentID)
); -- Create the Courses table
CREATE TABLE Courses ( CourseID INTEGER, CourseName VARCHAR(100), Credits INTEGER, PRIMARY KEY (CourseID)
); -- Create the Enrolments table to link Students and Courses
CREATE TABLE Enrolments ( StudentID INTEGER, CourseID INTEGER, EnrolDate DATE, PRIMARY KEY (StudentID, CourseID), FOREIGN KEY (StudentID) REFERENCES Students(StudentID), FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
Basic DML commands
Retrieving data
|
Command |
Purpose |
Example |
|---|---|---|
|
|
Retrieves specific columns from a table |
|
|
|
Filters results based on a condition |
|
|
|
Sorts the results in ascending or descending order |
|
|
|
Groups rows that have the same value in specified columns |
|
|
|
Combines rows from two tables where there is a matching key |
|
|
|
Calculates the total of a numeric column |
|
|
|
Counts the number of rows (or non-null entries in a column) |
|
|
|
Calculates the average value of a numeric column |
|
Data maintenance
|
Command |
Purpose |
Example |
|---|---|---|
|
|
Adds a new record to a table |
|
|
|
Removes records from a table |
|
|
|
Modifies existing data in a table |
|
Example: School database
-
You’re working with a school database containing three tables:
-
Students(StudentID, FirstName, LastName, Age) -
Courses(CourseID, CourseName, Credits) -
Enrolments(StudentID, CourseID, EnrolDate)
-
-
You want to query the database and maintain the data as students enrol in courses, update their details, or leave the school
Querying
-- 1. Get the first and last names of all students
SELECT FirstName, LastName FROM Students; -- 2. List students who are older than 16
SELECT * FROM Students WHERE Age > 16; -- 3. List all students sorted alphabetically by surname
SELECT * FROM Students ORDER BY LastName ASC; -- 4. Count how many students are enrolled on each course
SELECT CourseID, COUNT(*) FROM Enrolments GROUP BY CourseID; -- 5. Show student names with the courses they are enrolled on
SELECT Students.FirstName, Students.LastName, Courses.CourseName
FROM Students
INNER JOIN Enrolments ON Students.StudentID = Enrolments.StudentID
INNER JOIN Courses ON Enrolments.CourseID = Courses.CourseID; -- 6. Get the total number of course credits available
SELECT SUM(Credits) FROM Courses; -- 7. Get the average age of all students
SELECT AVG(Age) FROM Students;
Maintaining
-- 1. Add a new student to the Students table
INSERT INTO Students (StudentID, FirstName, LastName, Age)
VALUES (4, 'Amira', 'Patel', 17); -- 2. Enrol that student on CourseID 2
INSERT INTO Enrolments (StudentID, CourseID, EnrolDate)
VALUES (4, 2, '01/09/2025'); -- UK date format (DD/MM/YYYY) -- 3. Update a student's age
UPDATE Students SET Age = 18 WHERE StudentID = 4; -- 4. Delete a student who has left the school
DELETE FROM Students WHERE StudentID = 4;
Worked Example
Part of a database table BIRD_TYPE is shown:
|
|
|
|
|---|---|---|
|
0123 |
Blackbird |
Medium |
|
0035 |
Jay |
Large |
|
0004 |
Raven |
Large |
|
0085 |
Robin |
Small |
The database only supports these data types:
-
character
-
varchar
-
Boolean
-
integer
-
real
-
date
-
time
Write a Structured Query Language (SQL) script to define the table Bird_Type. [4]
Answer
-
CREATE TABLEstart and end bracket [1 mark] -
Bird ID as CHAR/VARCHAR [1 mark]
-
Name and size as VARCHAR/CHAR [1 mark]
-
Bird ID as primary key [1 mark]
Example answer
CREATE TABLE BIRD_TYPE(
BirdID CHAR(4) NOT NULL,
Name VARCHAR(9),
Size VARCHAR(6),
PRIMARY KEY (BirdID));
Responses