Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Key Revision Points.

Similar presentations


Presentation on theme: "SQL Key Revision Points."— Presentation transcript:

1 SQL Key Revision Points

2 SQL SQL (Structured Query language) pronounced “sequel”
Declarative language consisting of commands called SQL statements Statements usually written in CAPS Statements contain clauses – individual commands Can create, update, query databases using SQL

3 SELECT (SELECT…FROM…WHERE)
The SELECT clause is used to query (search) the database It SELECTS the records matching the criteria you specify

4 SELECT - Basic SELECT * FROM dogs SELECT dogName FROM dogs
Returns all the data from all the records in the table called dogs (uses a wildcard) SELECT dogName FROM dogs Returns just the name for all the records in the table called dogs SELECT dogName, dogBreed, Age FROM dogs Returns the name, breed and age for all the records in the table called dogs

5 SELECT - Advanced Records can be filtered using arithmetic and Boolean operators SELECT dogName, Age FROM dogs WHERE Age <=5 SELECT dogName FROM dogs WHERE dogBreed=“Jack Russell” SELECT dogName, dogColour FROM dogs WHERE Age<5 OR dogBreed=“Chihuahua” SELECT dogName, dogColour FROM dogs WHERE dogName=“Killer” AND dogBreed=“Chihuahua” Syntax in the WHERE clause the same as used in Python if/while statements i.e. standard arithmetic and Boolean operators

6 SELECT Syntax Note from previous examples that syntax is always the same: SELECT dogName, dogColour FROM dogs WHERE Age<5 OR dogBreed=“Chihuahua” What to fields to display in the results Table containing the data Criteria or conditions

7 INSERT (INSERT INTO…VALUES)
INSERT clause used to ADD records to an existing database

8 INSERT - Basic INSERT INTO dogs VALUES (“Psycho”, “Poodle”, “Grey”, 5)
Inserts the record into the dogs table

9 INSERT – Advanced/Syntax
If you want to add a partial record i.e. not all the fields you must also specify the field names after the table name INSERT INTO dogs(dogName, age) VALUES (“Mr Fluffy”,4) Which table/fields to insert data into What data to insert into these fields* * Must have the same number of fields/values and must be in the same order

10 UPDATE (UPDATE…SET…WHERE)
UPDATE clause used to change existing records in a database table.

11 UPDATE - Basic UPDATE dogs SET dogName=“Mr. Fuzzychops” WHERE dogName=“Bert” Changes Bert’s name to Mr. Fuzzychops UPDATE dogs SET dogName=“Jason Bieber” WHERE dogID=5 Dog #5’s name is set to Jason Bieber UPDATE dogs SET dogBreed=“Old Dog” WHERE age>10 AND dogBreed=“Alsatiopoodle” Boolean in where clause may change multiple records i.e. all alsatiopoodles over 10 years old

12 UPDATE - Syntax Syntax always the same
UPDATE dogs SET dogBreed=“Old Dog” WHERE age>10 AND dogBreed=“Alsatiopoodle” Table containing existing data Field to change and new data Criteria used to select the records to be changes

13 Sorting You can sort the results of a SELECT query using an ORDER BY clause SELECT dogName, dogColour FROM dogs WHERE Age<5 OR dogBreed=“Chihuahua” ORDER BY dogName ASC Use ASC to sort A-Z, 0-9 etc. Use DESC to sort Z-A, 9-0 etc.

14 Indexing (Create Index…On)
To add a named index CREATE INDEX dogIndex ON dogs(dogName)

15 Textbook Textbook pages 52, 53, 56


Download ppt "SQL Key Revision Points."

Similar presentations


Ads by Google