Intro and Syntax
SQL is a standard language for accessing and manipulating databases.
Data Definition Language or DDL deals with defining databases and the tables.
Data Manipulation Language or DML deals with the CRUD operations.
CRUD means Create, Read, Update, and Delete with each having SQL commands.
Database Tables
- each table identified by name
- tables contain records(rows) with data
SQL Statements
- are not case sensitive but convention is to write all keywords in uppercase
- each statement needs to end with a semicolon
Create
Create or add data to the database.
CREATE DATABASE
CREATE DATABASE dbname;
Used to create a database.
CREATE TABLE
CREATE TABLE table_name(column_name1 data_type(size),...);
Used to create a table in a database.
CREATE INDEX
CREATE INDEX index_name ON table_name (column_name);
Creates an index on a table and duplicate values are allowed.
CREATE UNIQUE INDEX
CREATE UNIQUE INDEX index_name ON table_name (column_name);
Creates a unique index on a table.
Constraints
Constraints are the rules enforced on data columns on tables and are used to limit the type of data that can go into a table.
- NOT NULL: ensures that a column cannot have NULL values
- PRIMARY KEY: uniquely identifies each rows/records in a database table
- FOREIGN KEY: uniquely identifies rows/records in any another database table
INSERT INTO
INSERT INTO table_name VALUES (value1,...);
Used to insert new records in a table.
Read
Searching, filtering, and limiting the result set.
SELECT
SELECT column_name,column_name FROM table_name;
and
SELECT * FROM table_name;
Used to select data from a database.
WHERE
SELECT column_name,column_name FROM table_name WHERE column_name operator value;
Used to filter records.
AND
SELECT column_name,column_name FROM table_name WHERE column_name operator value AND column_name operator value;
Operator displays a record if both the first condition and the second condition are true.
OR
SELECT column_name,column_name FROM table_name WHERE column_name operator value OR column_name operator value;
Operator displays a record if either the first condition or the second condition is true.
ORDER BY
SELECT column_name, column_name FROM table_name ORDER BY column_name ASC|DESC, column_name ASC|DESC;
Used to sort the result-set.
IN
SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...);
Operator allows you to specify multiple values in a WHERE clause.
BETWEEN
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
Operator selects values(numbers, text, or dates) within a range.
LIKE
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
Operator is used in a WHERE clause to search for a specified pattern in a column.
Wildcard Characters
Used with the LIKE operator to search for data within a table.
- %: subsitute for zero or more characters
- _: substitute for a single character
- [charlist]: sets and ranges of characters to match
- [^charlist] or [!charlist]: matches only a character not specified within the brackets