44 total
By the end of this subtopic, you should be able to:
understand that a DBMS uses Data Definition Language (DDL) to create and change the structure of a database
understand that a DBMS uses Data Manipulation Language (DML) to query, add, edit, and delete the data stored in a database
know that the industry-standard language for both DDL and DML is Structured Query Language (SQL)
understand a given SQL statement
understand and write simple DDL statements using:
CREATE DATABASECREATE TABLECHARACTER, VARCHAR(n), BOOLEAN, INTEGER, REAL, DATE, TIMEALTER TABLEPRIMARY KEY (field)FOREIGN KEY (field) REFERENCES Table(Field)write simple DML scripts to query or modify data in at most two tables using:
SELECT ... FROMWHEREORDER BYGROUP BYINNER JOINSUMCOUNTAVGINSERT INTODELETE FROMUPDATEWhen a database is used in a school, hospital, bank, or business, there are two different jobs that need to happen.
The first job is to build the database structure. This means creating the database itself, creating the tables, choosing field names, choosing data types, and setting keys such as primary keys and foreign keys.
The second job is to work with the data inside those tables. This means searching for records, adding new records, changing existing records, and deleting unwanted records.
A DBMS does both of these jobs, but it uses two different parts of SQL for them.
DDL is used for the structure of the database. DML is used for the contents of the database.
A simple way to remember this is:
The standard language used for both DDL and DML is SQL, which stands for Structured Query Language.
SQL has been used for many years and is accepted as the normal language for relational databases. Different database programs may have small differences in the way they write some commands, but the main ideas stay the same.
For your syllabus, you need to understand and write the SQL forms listed in the learning objectives.
Data Definition Language (DDL) is used to create, modify, and remove the data structures that make up a database.
This means DDL is used when you want to:
DDL does not deal with the actual records stored in the table. It deals with the structure only.
CREATE DATABASEThe CREATE DATABASE command is used to create a new database.
Example:
CREATE DATABASE School
This creates a new database called School.
You usually do this before creating any tables, because the tables need to be stored inside a database.
CREATE TABLEThe CREATE TABLE command is used to create a table definition.
A table definition includes:
Example:
CREATE TABLE Student(
StudentID CHARACTER,
FirstName CHARACTER,
SecondName CHARACTER,
DateOfBirth DATE,
ClassID CHARACTER
);
This command creates a table called Student.
It also creates five fields:
StudentIDFirstNameSecondNameDateOfBirthClassIDEach field is given a data type so that the DBMS knows what kind of data can be stored there.
A data type tells the DBMS what kind of value a field can hold. Choosing the correct data type is important because it helps the DBMS store and check data properly.
CHARACTERCHARACTER is used for fixed-length text.
This means the field is designed to store text of a set size. In simple terms, it is useful when all values are expected to be about the same length, such as codes or short IDs.
Example:
StudentID CHARACTER
VARCHAR(n)VARCHAR(n) is used for variable-length text.
The n shows the maximum number of characters allowed. This is useful for names, addresses, and other text where the length may change.
Example:
FirstName VARCHAR(50)
This means the field can store text up to 50 characters long.
BOOLEANBOOLEAN stores a true or false value.
In SQL, this is often represented using 1 and 0.
For example, a field such as PaidFees could store whether fees have been paid.
Example:
PaidFees BOOLEAN
INTEGERINTEGER is used for whole numbers.
It does not store decimal values.
Example:
Age INTEGER
REALREAL is used for numbers with decimal places.
Example:
AverageMark REAL
DATEDATE stores a date value.
A common format is:
YYYY-MM-DD
Example:
DateOfBirth DATE
TIMETIME stores a time value.
A common format is:
HH:MM:SS
Example:
StartTime TIME
ALTER TABLEThe ALTER TABLE command is used to change an existing table definition.
This means you can use it after a table has already been created.
For example, you may want to:
Example:
ALTER TABLE Student ADD PRIMARY KEY (StudentID)
This changes the Student table by making StudentID the primary key.
A primary key is a field that uniquely identifies each record in a table.
This means no two rows should have the same primary key value.
For example, in a student table, each student should have a different StudentID. That makes StudentID a good primary key.
Example:
ALTER TABLE Student ADD PRIMARY KEY (StudentID)
You may also see a primary key written inside a CREATE TABLE command.
Example:
CREATE TABLE Students (
StudentID INTEGER,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
PRIMARY KEY (StudentID)
);
A foreign key is a field in one table that links to the primary key in another table.
Foreign keys are used to create relationships between tables.
For example, if a student belongs to a class, then the ClassID in the Student table can refer to the ClassID in the Class table.
Example:
ALTER TABLE Student ADD FOREIGN KEY ClassID REFERENCES Class(ClassID)
This means the ClassID field in Student is linked to the ClassID field in Class.
That link helps the DBMS connect related data across tables.
Here is a simple DDL script for creating a school database:
CREATE DATABASE School
CREATE TABLE Student(
StudentID CHARACTER,
FirstName CHARACTER,
SecondName CHARACTER,
DateOfBirth DATE,
ClassID CHARACTER
);
ALTER TABLE Student ADD PRIMARY KEY (StudentID)
CREATE TABLE Class(
ClassID CHARACTER,
Location CHARACTER,
LicenceNumber CHARACTER
);
ALTER TABLE Class ADD PRIMARY KEY (ClassID)
ALTER TABLE Student ADD FOREIGN KEY ClassID REFERENCES Class(ClassID)
The order matters when writing a DDL script.
First, the database is created. Then the tables are created. Then primary keys are added. Then foreign keys are added after the referenced table already exists.
This matters because you cannot create a foreign key link to a table that has not been created yet.
Sign in to view full notes