zeit2301 design of information systems sql: creating a database school of engineering and...
TRANSCRIPT
![Page 1: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/1.jpg)
ZEIT2301Design of Information Systems
SQL: Creating a Database
School of Engineering and Information TechnologyUNSW@ADFA
Dr Kathryn Merrick
![Page 2: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/2.jpg)
Topic 10: SQL Basics
This lecture will introduce the SQL for Creating a database Inserting data Updating data Making simple queries
We will then compare how this is done in MS Access When can SQL be used? When is it hidden?
![Page 3: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/3.jpg)
Structured Query Language
SQL, the “Structured Query Language”, has become the standard language for data manipulation in relational databases
SQL is a non-procedural language - it specifies what is to be done, not how it is to be done
Relatively easy to use for simple queries some subtleties in more complex queries
![Page 4: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/4.jpg)
4
Statements in SQL’s Data Manipulation Language
INSERT – add data to a table
UPDATE – modify data in a table
DELETE – remove data from a table
SELECT – retrieve data from database
most commonly used statement (next lecture)
![Page 5: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/5.jpg)
Remember our bike example in 3NF
Bike name*
Number of riders*
Centre of mass height
Harley 1 0.724
Harley 2 0.775
Honda 1 0.831
Honda 2 0.881
Road conditions*
Coefficient of friction
Icy 0.1
Wet 0.5
Dry 0.9
Scenario ID*
Bike name
Number of riders
Road conditions
Can stoppie
1 Harley 1 Dry false
2 Harley 2 Dry false
3 Honda 1 Dry true
4 Honda 2 Dry true
Bike name* Wheelbase
Harley 1.588
Honda 1.458
![Page 6: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/6.jpg)
Creating Relations: Example 1
CREATE TABLE
Bikes (bikeName VARCHAR(60) NOT NULL UNIQUE,
wheelbase DECIMAL(4, 3) NOT NULL,
PRIMARY KEY (bikeName));
![Page 7: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/7.jpg)
Common SQL Data Types
Data Type Description
INTEGER Up to 11 digits; –ve sign takes one place.
SMALLINT Up to 6 digits, including sign.
NUMERIC(s,d) Fractional numbers, with a maximum of s digits and d decimal places; d can be omitted.
DECIMAL(s,d) Fractional numbers, with a minimum of s digits and d decimal places; d and s can be omitted.
CHAR(s) Character strings, with s places provided.
VARCHAR(s) Unpadded character strings, up to length s.
DATE Ten character date representation (year month day) as digits.
BOOLEAN Boolean
![Page 8: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/8.jpg)
Creating Relations: Example 2
CREATE TABLE
CoMHeight( bikeName VARCHAR(60) NOT NULL UNIQUE,
numRiders INTEGER NOT NULL
comHeight DECIMAL(4, 3) NOT NULL,
CONSTRAINT pk2 PRIMARY KEY (bikeName)
CONSTRAINT fk1 FOREIGN KEY (bikeName) REFERENCES Bikes
ON UPDATE CASCADE ON DELETE NO ACTION);
![Page 9: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/9.jpg)
SQL ConstraintsConstraint Type Description
CONSTRAINT… PRIMARY KEY… • Supports entity integrity. • Defines the primary key column(s). Must
be NOT NULL and UNIQUE
CONSTRAINT…FOREIGN KEY… ON UPDATE … ON DELETE…
• Supports referential integrity• Regulates update/delete operations that
have implications to other relations
Foreign Key Subclause Values
CASCADE Update/delete rows from parent table and automatically update/delete matching rows in child tables
SET NULL Update/delete rows from parent table and set the foreign key values in child table to NULL
NO ACTION Reject the update/delete operation
![Page 10: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/10.jpg)
Inserting Records into a Relation
INSERT INTO Bikes VALUES (‘Harley’, 1.588);
INSERT INTO Bikes VALUES (‘Honda’, 1.458);
INSERT INTO Bikes VALUES(‘Mongoose Expert’, 0.932);
INSERT INTO CoMHeight VALUES(‘Harley’, 0, 0.724);
![Page 11: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/11.jpg)
Inserting Records From a File
It may not be practical to write insert statements for every record
To read multiple records from a file in bulk:
LOAD DATA LOCAL INFILE filename INTO TABLE Bikes
FIELDS TERMINATED BY ‘\t’;
![Page 12: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/12.jpg)
Viewing Records: Simple SELECT
SELECT * FROM Bikes;
SELECT scenarioID, canStoppie FROM Scenarios;
SELECT * FROM CoMHeight WHERE bikeName = ‘Harley’;
SELECT * FROM CoMHeight WHERE numRiders > 1;
![Page 13: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/13.jpg)
Modifying Records
UPDATE Bikes SET wheelbase = 1.6
WHERE bikeName = ‘Harley’
UPDATE CoMHeight SET comHeight = 0.746
WHERE bikeName = ‘Harley’ AND numRiders = 1;
![Page 14: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/14.jpg)
Deleting Records
DELETE FROM CoMHeight WHERE bikeName = ‘Honda’;
DELETE FROM CoMHeight WHERE numRiders = 2;
![Page 15: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/15.jpg)
MS Access
![Page 16: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/16.jpg)
![Page 17: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/17.jpg)
![Page 18: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/18.jpg)
![Page 19: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/19.jpg)
![Page 20: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/20.jpg)
![Page 21: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/21.jpg)
![Page 22: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/22.jpg)
![Page 23: ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick](https://reader035.vdocuments.site/reader035/viewer/2022062718/56649e665503460f94b6066e/html5/thumbnails/23.jpg)
Summary
After today’s lecture you should be able to: Create and update a relational database using SQL