5.3. Querying a Database SQL -> DDL, DML
SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL version developed at IBM by Chamberlin and Boyce in early 1970s Declarative language concerned with ‘what’ rather than ‘how’. Mainly used to query a database but also used to create tables
DML Data Manipulation Language Part of SQL concerned with asking questions of a database Eg. SELECT.. FROM.. DDL Data Definition Language A language to define the structure and instances of a database E.g. CREATE TABLE employee (EmpID INT, NameVARCHAR (10), HiredDate DATE, Salary CURRENCY);
Data Manipulation Language (DML) Exercises are based on the Orders database in chapter 5.3. Make a copy of the database in your N drive.
tblsoftware Licence no Customer id Packageversionprice Service agreement Date of purchase 1000RentAPayroll4.0£550.00Yes18/02/ SeymourAccounts6.1£475.00No01/07/ RentAStock2.0£700.00Yes13/07/ SeymourStock2.0£770.00Yes06/11/ RedcabsPayroll5.0£620.00Yes05/12/ Supagstock6.2£900.00No14/02/ RedcabsAccounts6.2£520.00Yes14/02/ PradeshPayroll5.0£620.00Yes 7114RentaAccounts6.2£500.00Yes17/03/2000 Customers Customer IDCompany NameContact Last NamePhone Number Pradeshpradesh&Co LtdKarl Pradesh(0176) RedcabsRedcabs ltdFred Gordon(0181) RentaRent-A-ToolMark Wong(0147) SeymourSeymour GlassJames Bolan(0135) SupagSupa-goodMavis Hunt(0120)
SELECT.. FROM.. WHERE SELECT field1, field2 FROM tablename; SELECT DISTINCT FROM tablename; SELECT Package,version FROM tblsoftware WHERE Package='Payroll';
GROUP BY.. SELECT SUM(price) AS SumOfPrice FROM tblSoftware WHERE CustomerID = “REDCABS”;
Extract data from more than one table SELECT tblname1.fieldname, tblname2.field2 FROM tblname1, tblname2 WHERE tblname1.field1 = tblname2.field1;
DATA DEFINITION LANGUAGE Defines the logical structure and files within the database. Example: CREATE TABLE staff (StaffId INT NOT NULL, StaffName VARCHAR(10), Dept VARCHAR(5), PRIMARY KEY (StaffID), FOREIGN KEY (Dept) REFERENCES Department(DeptId));
CREATE DATABASE studentsDB; ALTER TABLE tblSoftware ADD StaffId INT; CREATE INDEX NamePackage ON tblSoftware(Package);
GRANT all PRIVILEGES ON studentsDB.* TO IDENTIFIED BY ‘password’;
Exercise Resources To practice SQL queries