sql assessment command statements

30
SQL Assessment Commands

Upload: shaunwilson10

Post on 22-Jan-2018

71 views

Category:

Education


1 download

TRANSCRIPT

Page 1: SQL Assessment Command Statements

SQL Assessment Commands

Page 2: SQL Assessment Command Statements

Query Command Statement Examples

• SELECT• INSERT• AND• FROM• IF• WHERE• AS• OR• DELETE• DROP• ALTER

Page 3: SQL Assessment Command Statements

Datatype Examples

• CHAR

• VARCHAR

• INT

• MEDIUMINT

• TEXT

• BLOB

• MEDIUMBLOB

Page 4: SQL Assessment Command Statements

SELECT Command Statement

• The SELECT statement is used to select data from a database

Examples:

The following SQL statement selects the "CustomerName" and "City" columns from the "Customers" table:

SELECT CustomerName, City FROM Customers;

The following SQL statement selects all the columns from the "Customers" table:

SELECT * FROM Customers;

Page 5: SQL Assessment Command Statements

WHERE Command Statement

• The WHERE statement is used to filter records. The WHERE clause is used to extract records that fulfil a specified criteria only.

Examples:

The following SQL statement selects all the customers from the country "Mexico", in the "Customers" table:

SELECT * FROM CustomersWHERE Country='Mexico’;

However, numeric fields should not be enclosed in quotes:

SELECT * FROM CustomersWHERE CustomerID=1;

Page 6: SQL Assessment Command Statements

WHERE Clause Operators

Operator Description

= Equal

<> Not Equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive range

LIKE Search for a pattern

IN To specify multiple possible values for a column

Page 7: SQL Assessment Command Statements

AND Command Statement

• The AND and OR operators are used to filter records based on more than one condition:• The AND operator displays a record if all the conditions separated by AND is TRUE:

Example:

The following SQL statement selects all fields from "Customers" where country is "Germany" AND city is "Berlin":

SELECT * FROM CustomersWHERE Country='Germany' AND City='Berlin';

Page 8: SQL Assessment Command Statements

OR Command Statement

• The AND and OR operators are used to filter records based on more than one condition:• The OR operator displays a record if any of the conditions separated by OR is TRUE.

Examples:

The following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":

SELECT * FROM CustomersWHERE City='Berlin' OR City='München';

Page 9: SQL Assessment Command Statements

NOT Command Statement

• The NOT operator displays a record if the condition(s) is NOT TRUE:

Examples:

The following SQL statement selects all fields from "Customers" where country is NOT "Germany":

SELECT * FROM CustomersWHERE NOT Country='Germany';

Page 10: SQL Assessment Command Statements

Combining AND, OR and NOT operators

• You can also combine the AND, OR and NOT operators:

Examples:

The following SQL statement selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "München":

SELECT * FROM CustomersWHERE Country='Germany' AND (City='Berlin' OR City='München’);

The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":

SELECT * FROM CustomersWHERE NOT Country='Germany' AND NOT Country='USA';

Page 11: SQL Assessment Command Statements

INSERT INTO Command Statement

• The INSERT INTO statement is used to insert new records in a table:

Syntax:

INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);

Examples:

The following SQL statement inserts a new record in the "Customers" table:

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

Page 12: SQL Assessment Command Statements

INSERT INTO only specified table columns

• It is also possible to only insert data in specific columns.

Examples:

The following SQL statement will insert a new record, but only insert data in the "CustomerName", "City", and "Country" columns:

INSERT INTO Customers (CustomerName, City, Country)VALUES ('Cardinal', 'Stavanger', 'Norway’);

Page 13: SQL Assessment Command Statements

SQL NULL Values

• A field with a NULL value is a field with no value.

Note:

A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation.

Page 14: SQL Assessment Command Statements

UPDATE Command Statement

• The UPDATE statement is used to modify the existing records in a table.

Example:

The following SQL statement updates the first customer (CustomerID = 1) with a new contact person and a new city (Alfred and Frankfurt are new values):

UPDATE CustomersSET ContactName = 'Alfred Schmidt', City= 'Frankfurt'WHERE CustomerID = 1;

Page 15: SQL Assessment Command Statements

UPDATE Multiple Records

• It is the WHERE clause that determines how many records that will be updated:

Example:The following SQL statement will update the contactname to "Juan" for all records where country is "Mexico":

UPDATE CustomersSET ContactName='Juan'WHERE Country='Mexico’;

Note: Missing out the WHERE statement will alter every value under SET

Page 16: SQL Assessment Command Statements

DELETE Command Statement

• The DELETE statement is used to delete existing records in a table.

Example:The following SQL statement deletes the customer “John Smith" from the "Customers" table:

DELETE FROM CustomersWHERE CustomerName=‘John Smith’;

Note: To delete all from a table, use:

DELETE * FROM table_name;

Page 17: SQL Assessment Command Statements

CREATE DATABASE Command Statement

• The CREATE DATABASE statement is used to create a new SQL database:

Syntax:

CREATE DATABASE databasename;

Example:

CREATE DATABASE testDB;

Page 18: SQL Assessment Command Statements

DROP DATABASE Command Statement

• The DROP DATABASE statement is used to drop an existing SQL database:

Syntax:

DROP DATABASE databasename;

Example:

DROP DATABASE testDB;

Page 19: SQL Assessment Command Statements

CREATE TABLE Command Statement

• The CREATE TABLE statement is used to create a new table in a database:

Syntax:

CREATE TABLE table_name (column1 datatype,column2 datatype,column3 datatype,

....);

Page 20: SQL Assessment Command Statements

CREATE TABLE continued…

Example:

CREATE TABLE testtable (PersonID int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255)

);

Page 21: SQL Assessment Command Statements

DROP TABLE Command Statement

• The DROP TABLE statement is used to drop an existing table in a database.

Syntax:

DROP TABLE table_name;

Example:

DROP TABLE testtable;

Page 22: SQL Assessment Command Statements

TRUNCATE TABLE Command Statement

• The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE testtable;

Page 23: SQL Assessment Command Statements

ALTER TABLE Command Statement

• The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

• The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

Syntax:

To add a column in a table, use the following syntax:

ALTER TABLE table_nameADD column_name datatype;

Page 24: SQL Assessment Command Statements

ALTER TABLE continued…

Syntax:

To delete a column in a table, use the following syntax:

ALTER TABLE table_nameDROP COLUMN column_name;

Examples:

ALTER TABLE testtableADD DateOfBirth date;

Page 25: SQL Assessment Command Statements

ALTER TABLE continued…

Examples:The following deletes the column named "DateOfBirth" in the “testtable" table:

ALTER TABLE PersonsDROP COLUMN DateOfBirth;

The following changes the data type of the column named "DateOfBirth" in the “testtable" table (date to year):

ALTER TABLE PersonsALTER COLUMN DateOfBirth year;

Page 26: SQL Assessment Command Statements

SQL Constraints Examples

• SQL constraints are used to specify rules for the data in a table:

• The following constraints are commonly used in SQL:• NOT NULL - Ensures that a column cannot have a NULL value• UNIQUE - Ensures that all values in a column are different• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely

identifies each row in a table• FOREIGN KEY - Uniquely identifies a row/record in another table• CHECK - Ensures that all values in a column satisfies a specific condition• DEFAULT - Sets a default value for a column when no value is specified• INDEX - Used to create and retrieve data from the database very quickly

Page 27: SQL Assessment Command Statements

PRIMARY KEY Constraint• The PRIMARY KEY constraint uniquely identifies each record in a database

table.

• Primary keys must contain UNIQUE values, and cannot contain NULL values.

PRIMARY KEY on CREATE TABLE:

CREATE TABLE testtable (ID int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int,PRIMARY KEY (ID)

);

Page 28: SQL Assessment Command Statements

PRIMARY KEY continued…

PRIMARY KEY on ALTER TABLE:

ALTER TABLE testtableADD PRIMARY KEY (ID);

DROP a PRIMARY KEY:

ALTER TABLE testtableDROP PRIMARY KEY;

Page 29: SQL Assessment Command Statements

FOREIGN KEY Constraint

• A FOREIGN KEY is a key used to link two tables together.• A FOREIGN KEY is a field (or multiple) in one table that refers to the

PRIMARY KEY in another table:

FOREIGN KEY on CREATE TABLE:CREATE TABLE Orders (

OrderID int NOT NULL,OrderNumber int NOT NULL,PersonID int,PRIMARY KEY (OrderID),FOREIGN KEY (PersonID) REFERENCES testtable(PersonID)

);

Page 30: SQL Assessment Command Statements

FOREIGN KEY continued…

FOREIGN KEY on ALTER TABLE:

ALTER TABLE OrdersADD FOREIGN KEY (PersonID) REFERENCES testtable(PersonID);

DROP a FOREIGN KEY:

ALTER TABLE OrdersDROP FOREIGN KEY FK_PersonOrder;