You are on page 1of 7

Summary of DML and DDL

Commands
CAUTION!
Use these Notes along with the main notes as well.
Summary of DDL Commands

Summary of DML Commands


Here are some examples of DDL that could have been used The following DDL command will add a field
when the school database was created. by the name of “Returned” of Type
BOOLEAN to the table “DEVICE”.
Here are some examples of DML that could have been used to query and
update the school
database.

This query will show, in alphabetical order of second name, the first and
second names of all students in class 7A:

This query will show the teacher’s name and the subject taught:

An alternate answer the the above can be:


SELECT Teacher.TeacherName,Subject.SubectName
FROM Teacher,Subject
WHERE Teacher.LicenceNumber=Subject.LicenceNumber;
DML Examples Continued:-

This statement will insert a row into the Student table:

A safe method can be used if the columns are not known:


INSERT INTO Student (StudentID,FirstName,SecondName,DateOfBirth,ClassID)
VALUES (S1301,Peter,Probert,06/06/2011,7A);

These statements will delete the specified row(s) from the Student table (take care only: DELETE FROM Student will
delete the whole table!):

Considering “REFERENTIAL INGTEGRITY” if a record is being


deleted, the primary key of which is a foreign key of another
DELETE FROM Band-Booking table. The records where the references exist must be
WHERE BandName = ‘ITWizz’; deleted first. For example here, The goal is to delete a band
from BAND table. But that band is also a FK in BAND-
DELETE FROM Band BOOKING table therefore it is deleted from there first and
WHERE BandName = ‘ITWizz’; then from BAND table.
DML Examples Continued:-

Another type of SELECT statement is known as Aggregate Function which can evaluate the one
attribute amongst several records to return one value.

8SELECT COUNT(BandName) Counts the Number of bands (i.e. records in table) using primary key for that table (BandName)
FROM Band;
9On the other hand, aggregate functions require the specification of an attribute. Like in this instruction we
9SELECT AVG(NumberOfMembers) are finding the average number of members of ALL bands using AVG command with NumberOfMembers
FROM Band; specified.
10SELECTSUM(NumberOfMembers) 10Similarily SUM will return the total number of members for all bands.
FROM Band;
Using the “LIKE” keyword
The following SQL selects all customers with a
CustomerName starting with "a":

The following SQL selects all customers with a


CustomerName ending with "a":

The following SQL selects all customers with a


CustomerName that have "or" in any position:
DML Examples Continued:-

1SELECT 1As it can be seen the attributes that need to be fetched are mentioned after the SELECT statement and the
BandName FROM Band;
table from which they are to be fetched comes after the FROM keyword.
2SELECT BandName, NumberOfMembers 2We can also retrieve data from multiple fields using comma between attributes.
FROM Band;
3Or we can simply use the “*” symbol to output data from ALL fields from the BAND table.
3SELECT * FROM Band;
4We can also ensure that the results are presented according to some order. In this case the result is to be
4SELECT BandName, NumberOfMembers
FROM Band presented in the order of BandName (i.e. alphabetical order due to BandName being text). There will be no
ORDER BY BandName; duplication of BandName as it is a primary key.
5SELECT BandName 5Inthe Band-Booking Table the attribute BandName may occur many times. To present the names of bands that
FROM Band-Booking have been booked this query can be used. However, the GROUP BY keyword prevents the BandName being
GROUP BY BandName; repeated on output.
6SELECT BandName 7SELECT BandName, NumberOfMembers 7Thisensures that only BandName
6A further extension to the above
FROM Band-Booking query allows us to add a criteria using FROM Band and NumberOfMembers are returned
WHERE Headlining = ‘Y’ the WHERE clause. This query will WHERE NumberOfMembers > 2 from Band table that have more than
GROUP BY BandName; produce a single output for each band ORDER BY BandName; two members.
that has headlined.

You might also like