Download SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Clusterpoint wikipedia , lookup

SQL wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
SQL
Key Revision Points
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
SELECT (SELECT…FROM…WHERE)
 The SELECT clause is used to query (search) the database
 It SELECTS the records matching the criteria you specify
SELECT - Basic
 SELECT * 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
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
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
INSERT (INSERT INTO…VALUES)
 INSERT clause used to ADD records to an existing database
INSERT - Basic
 INSERT INTO dogs VALUES (“Psycho”, “Poodle”, “Grey”, 5)
 Inserts the record into the dogs table
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
UPDATE (UPDATE…SET…WHERE)
 UPDATE clause used to change existing records in a database table.
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
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
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.
Indexing (Create Index…On)
 To add a named index
 CREATE INDEX dogIndex ON dogs(dogName)
Textbook
 Textbook pages 52, 53, 56