

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A step-by-step tutorial on creating a table named retailoutlet in a db called petersretaildb using sql commands. It covers creating the table schema, inserting records, and querying the data using select, where, and, or, order by, count(), sum(), max(), min(), group by, and other sql keywords.
Typology: Lecture notes
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























(^) This lecture will introduce some simple aspects of SQL (Structured Query Language) (^) You will not ‘know’ SQL at the end of this lecture (^) You will need to attend database courses to get a good/deep understanding (^) SQL is the industry standard for DB manipulation (^) At the end of the lecture you will be able to perform tasks via SQL scripts that in the past you may have used the GUI of your DB for. (^) SQL is commonly used from within another language (C++, Java…) where it is used to access a DB (^) These slides and accompanying .doc concentrate on the Apache Derby Database (which is firmly coupled to java).
There are many SQL keywords we could use to manipulate a table some of which are: CREATE, SELECT, INTO, UPDATE, FROM, WHERE, AND, OR, ORDER, BY, ASC, DESC, GROUP, COUNT(), SUM(), INSERT, VALUES, SET, DROP, TRIM, INT, REAL, DATE, CHAR, NOT, NULL, CHECK, DEFAULT, VIEW, DELETE, JOIN …
outletName WeeklySales Date TopsToGo £1500 2016-12- BottomsRUS £250 2016-12- HatRack £300 2016-12- Gizzmos £700 2016-12- We would need to: 1 Connect to the DB system and Create PetersRetailDb 3 Create the RetailOutlet table 4 Insert the four records into the table
(^) Syntax: (^) CREATE TABLE «TableName» (« Fieldname1» «Type1» [«constraints1»] [, « Fieldname2» «Type2» [«constraints2» ]]…); (^) Example: (^) CREATE TABLE RetailOutlet (OutletName Char(20) NOT NULL, WeeklySales real CHECK (WeeklySales > 0.00) DEFAULT 0.01, Date Date); (^) Note: (^) The default of 0.01 above is unrealistic/stupid (it is just a demo) (^) Some databases would have a problem (get confused) with a fieldname of Date for a field (Date being a keyword for the type and here also used as a name for a field)
(^) Syntax: (^) INSERT INTO «TableName »( «FieldName1» [, «FieldName2»]… ) VALUES («FieldValue1» [, «FieldValue2»]… ) ; (^) Example: (^) INSERT INTO RetailOutlet (OutletName, WeeklySales, Date ) VALUES ('TopsToGo',1500,'2016-12-03'); INSERT INTO RetailOutlet (OutletName, WeeklySales, Date ) VALUES ('BottomsRUs',250,'2016-12-10'); INSERT INTO RetailOutlet (OutletName, WeeklySales, Date ) VALUES ('HatRack',300,'2016-12-17'); INSERT INTO RetailOutlet (OutletName, WeeklySales, Date ) VALUES ('Gizzmos',700,'2016-12-17'); INSERT INTO RetailOutlet (outletName, WeeklySales, Date) VALUES ('TopsToGo', 900, '2016-12-17'); (^) Note: (^) You need one INSERT INTO per row/record 09/25/2019 Peter Dench 8
outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
(^) Syntax: SELECT «column_name» [,«column_name2»]… FROM «table_name» [WHERE «condition»] ORDER BY «column_name1» [ASC | DESC] [, «column_name2» [ASC | DESC]]…; Example: SELECT outletName, WeeklySales, Date FROM RetailOutlet ORDER BY WeeklySales DESC; ASC (default) could be used in a similar way outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
Syntax: SELECT «column_name1», SUM(«column_name2») FROM «table_name» GROUP BY «column_name1» ; (^) Example: SELECT outletName, SUM(WeeklySales) FROM RetailOutlet GROUP BY outletName ; outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/
Syntax: (^) UPDATE «table_name» SET «column_1» = «value1»[, «column_2» = «value2»]… WHERE «condition» ; Example: (^) UPDATE RetailOutlet SET WeeklySales = 500 WHERE outletName = ‘TopsToGo’ AND Date = ‘2016-12-17’ ; outletName WeeklySales Date TopsToGo £1500 03/Dec/ BottomsRUS £250 10/Dec/ HatRack £300 17/Dec/ Gizzmos £700 17/Dec/ TopsToGo £900 17/Dec/