SQL Tutorial Flashcards
Source: http://www.w3schools.com/sql/default.asp
What is SQL?
A standard language for accessing databases. It stands for Structured Query Language. It is an ANSI standard however there are different versions of the language.
What is ANSI?
American National Standards Institute
What things do you need to build a website that shows data from a database?
- An RDBMS database program
- To use a server-side scripting language like PHP or ASP
- To use SQL to get the data you want
- To use HTML/CSS
What is RDBMS?
Relational Database Management System.
Is SQL case sensitive?
No. SELECT is the same as select but normally SQL keywords are written in uppercase.
How is a SQL statement ended?
With a semi-colon ; Some databases require a semicolon at the end of each SQL statement. It is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
A database contains one or more _____?
Tables
A table is identified by a _____?
Name
How would you select all records in a table called “Customers” ?
SELECT * FROM Customers;
Pay attention to syntax!
What are the 11 “Most Important SQL Commands?”
SELECT
UPDATE
DELETE
INSERT INTO
CREATE DATABASE
ALTER DATABASE
CREATE TABLE
ALTER TABLE
DROP TABLE
CREATE INDEX
DROP INDEX
What does SELECT do?
extracts data from a database
What does UPDATE do?
updates data in a database
What does DELETE do?
deletes data (rows) from a database
What does INSERT INTO do?
inserts new records into a database
What does CREATE DATABASE do?
creates a new database
What does ALTER DATABASE do?
modifies a database
What does CREATE TABLE do?
creates a new table
What does ALTER TABLE do?
modifies a table
What does DROP TABLE do?
deletes a table
What does CREATE INDEX do?
creates an index (search key)
What does DROP INDEX do?
deletes an index
What statement creates a new table?
CREATE TABLE
What statement deletes a table?
DROP TABLE
What statement creates a search key?
CREATE INDEX
What statement deletes data from a database?
DELETE
What statement extracts data from a database?
SELECT
What statement updates data in a database?
UPDATE
What statement inserts new data?
INSERT INTO
What statement deletes an index?
DROP INDEX
What statement creates a database?
CREATE DATABASE
What statement modifies a table?
ALTER TABLE
What statement modifies a database?
ALTER DATABASE
What happens to the data from a SELECT statement?
The results are stored in a result table, called the result-set.
What is the complete syntax for a SELECT statement?
SELECT column_name, column_name
FROM table_name;
and
SELECT * FROM table_name;
What is the statement to select the CustomerName and City columns from the Customers table?
SELECT CustomerName, City
FROM Customers;
What does
SELECT * FROM Customers;
do?
It selects all columns from the Customers table.
How do you navigate within a result set?
Most database systems allow navigation in the result-set with programming functions, like: Move-To-First-Record, etc. Programming functions like these are not part of this SQL tutorial. To learn about accessing data with function calls they have ASP and PHP tutorials.
How do you return only different values? What example was used on the “Customer” table?
SELECT DISTINCT
SELECT DISTINCT City FROM Customers;
It returns a list of cities in the Customer table, but only once, even if several customers are in the same city.
How do you filter records? What is the syntax?
WHERE
SELECT column_name, column_name
FROM table_name
WHERE column_name operator value;
How would you find customers in the database who are in Mexico? Remember all the syntax.
SELECT * FROM Customers
WHERE Country=’Mexico’;
How are text fields and numeric fields handled differently?
SQL requires single quotes around text values (e.g. Country=’Mexico’). Most databases will allow double quotes.
Numeric field values should not be enclosed in quotes (e.g. CustomerID=1)
What 9 operators can be used in the WHERE clause?
If you have more than one condition what are the available operators and what do they mean?
The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.
How would you select customers from only the Berlin in Germany? Remember syntax.
SELECT * FROM Customers
WHERE Country=’Germany’ AND City=’Berlin’;
How would you select customers that are either in Berlin or Munchen?
SELECT * FROM Customers
WHERE City=’Berlin’ OR City=’Munchen’;
How would you select customers that are in Germany and either in Berlin or Munchen? (No Berlins or Munchens outside of Germany)
SELECT * FROM Customers
WHERE Country=’Germany’ AND (City=’Berlin’ OR City=’Munchen’);
What keyword sorts the result-set?
ORDER BY
What is the default order?
ORDER By sorts records in ascending order by default.