5.3. querying a database sql -> ddl, dml. sql structured query language – industry standard...

12
5.3. Querying a Database SQL -> DDL, DML

Upload: rudolph-newton

Post on 27-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

5.3. Querying a Database

SQL -> DDL, DML

Page 2: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

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

Page 3: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

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,Name VARCHAR (10),HiredDate DATE,Salary CURRENCY);

Page 4: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

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.

Page 5: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

tblsoftware

Licence no

Customer id

Package version priceService

agreementDate of

purchase

1000 RentA Payroll 4.0 £550.00 Yes 18/02/1999

1123 Seymour Accounts 6.1 £475.00 No 01/07/1999

2111 RentA Stock 2.0 £700.00 Yes 13/07/1999

3456 Seymour Stock 2.0 £770.00 Yes 06/11/1999

4870 Redcabs Payroll 5.0 £620.00 Yes 05/12/1999

5268 Supag stock 6.2 £900.00 No 14/02/2000

5381 Redcabs Accounts 6.2 £520.00 Yes 14/02/2000

6001 Pradesh Payroll 5.0 £620.00 Yes

7114 Renta Accounts 6.2 £500.00 Yes 17/03/2000

Customers

Customer ID Company Name Contact Last Name Phone Number

Pradesh pradesh&Co Ltd Karl Pradesh (0176) 3396018

Redcabs Redcabs ltd Fred Gordon (0181) 8799655

Renta Rent-A-Tool Mark Wong (0147) 3212777

Seymour Seymour Glass James Bolan (0135) 4543666

Supag Supa-good Mavis Hunt (0120) 2888557

Page 6: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

SELECT .. FROM .. WHERE

SELECT field1, field2 FROM tablename;

SELECT DISTINCT FROM tablename;

SELECT Package,version

FROM tblsoftware

WHERE Package='Payroll';

Page 7: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

GROUP BY ..

SELECT SUM(price) AS SumOfPrice FROM tblSoftware

WHERE CustomerID = “REDCABS”;

Page 8: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

Extract data from more than one table

SELECT tblname1.fieldname , tblname2.field2

FROM tblname1, tblname2

WHERE tblname1.field1 = tblname2.field1;

Page 9: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

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));

Page 10: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

CREATE DATABASE studentsDB;

ALTER TABLE tblSoftware

ADD StaffId INT;

CREATE INDEX NamePackage

ON tblSoftware(Package);

Page 11: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

GRANT all PRIVILEGES

ON studentsDB.*

TO ‘username’@’172.16.%.%’

IDENTIFIED BY ‘password’;

Page 12: 5.3. Querying a Database SQL -> DDL, DML. SQL Structured Query Language – industry standard supported by all significant relational databases. First SQL

Exercise Resources

To practice SQL querieshttp://sqlzoo.net/