Archive | SQL RSS feed for this section

SQL Basic Commands

18 Dec

SQL -> Structured Query Language

SQL lets you access and manipulate databases

Command functions

* SELECT – extracts data from a database
* UPDATE – updates data in a database
* DELETE – deletes data from a database
* INSERT INTO – inserts new data into a database
* CREATE DATABASE – creates a new database
* ALTER DATABASE – modifies a database
* CREATE TABLE – creates a new table
* ALTER TABLE – modifies a table
* DROP TABLE – deletes a table
* CREATE INDEX – creates an index (search key)
* DROP INDEX – deletes an index

SELECT Syntax
SELECT column_name(s) (Type the name of the column)
FROM table_name (Type the name of the table)

DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name

WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value

INSERT INTO Syntax
INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,…
WHERE some_column=some_value

DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value

(example)
DELETE FROM Car (Table name)
WHERE LastName=’City’ AND FirstName=’Honda’

INNER JOIN Syntax
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

LEFT JOIN Syntax
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

RIGHT JOIN Syntax
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

FULL JOIN Syntax
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PRIMARY KEY
CREATE TABLE_NAME
(
Column_name1 int NOT NULL PRIMARY KEY,
Column_name2 varchar(255) NOT NULL,
Column_name3 varchar(255),
Column_name4 varchar(255),
Column_name5 varchar(255)
)

FOREIGN KEY

A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

CREATE TABLE_NAME
(
Column_name1 int NOT NULL PRIMARY KEY,
Column_name2 int NOT NULL,
Column_name3 int FOREIGN KEY REFERENCES Table_name1.(Column_name1)
)