You are on page 1of 5

csb3d 5:00-6:00 MWF

Name: Nathallie H. Cabaluna


Acct. # 14
Project 2:

SQL Commands
Basic SQL
Each record has a unique identifier or primary key. SQL, which stands for Structured Query Language, is used to
communicate with a database. Through SQL one can create and delete tables. Here are some commands:
CREATE TABLE - creates a new database table
ALTER TABLE - alters a database table
DROP TABLE - deletes a database table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
SQL also has syntax to update, insert, and delete records.
SELECT - get data from a database table
UPDATE - change data in a database table
DELETE - remove data from a database table
INSERT INTO - insert new data in a database table
SELECT
The SELECT is used to query the database and retrieve selected data that match the specific criteria that you
specify:

SELECT column1 [, column2, ...]


FROM tablename
WHERE condition

The conditional clause can include these operators


= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal to
LIKE pattern matching operator
SELECT * FROM tablename

returns all the data from the table.


Use single quotes around text values (most database systems will also accept double quotes). Numerical values
should not be enclosed in quotes.
LIKE matches a pattern. The wildcard % is used to denote 0 or more characters.
'A%' : matches all strings that start with A
'%a' : matches all strings that end with a
'%a%' : matches all strings that contain an a

CREATE TABLE
The CREATE TABLE statement is used to create a new table. The format is:
CREATE TABLE tablename
(column1 data type,
column2 data type,
column3 data type);
char(size): Fixed length character string.
varchar(size): Variable-length character string. Max size is specified in parenthesis.
number(size): Number value with a max number of columns specified in parenthesis
date: Date value
number(size,d): A number with a maximum number of digits of "size" and a maximum number of "d" digits to the
right of the decimal

INSERT VALUES
Once a table has been created data can be inserted using INSERT INTO command.

INSERT INTO tablename


(col1, ... , coln)
VALUES (val1, ... , valn)

UPDATE
To change the data values in a pre existing table, the UPDATE command can be used.

UPDATE tablename
SET colX = valX [, colY = valY, ...]
WHERE condition

DELETE
The DELETE command can be used to remove a record(s) from a table.

DELETE FROM tablename


WHERE condition

To delete all the records from a table without deleting the table do

DELETE * FROM tablename

DROP
To remove an entire table from the database use the DROP command.

DROP TABLE tablename

ORDER BY
ORDER BY clause can order column name in either ascending (ASC) or descending (DESC) order.

ORDER BY col_name ASC

AND / OR
AND and OR can join two or more conditions in a WHERE clause. AND will return data when all the conditions are
true. OR will return data when any one of the conditions is true.

IN
IN operator is used when you know the exact value you want to return for at least one of the columns

SELECT * FROM table_name WHERE col_name IN (val1, val2, ...)


BETWEEN / AND
The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers, text, or
dates.

SELECT * FROM table_name WHERE col_name BETWEEN val1 AND val2

SQL Syntax
SQL is followed by unique set of rules and guidelines called Syntax. This tutorial gives you a quick start with SQL by
listing all the basic SQL Syntax:
All the SQL statements start with any of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE,
USE, SHOW and all the statements end with a semicolon (;).
Important point to be noted is that SQL is case insensitive, which means SELECT and select have same meaning in
SQL statements, but MySQL makes difference in table names. So if you are working with MySQL, then you need to
give table names as they exist in the database.

SQL SELECT Statement:


SELECT column1, column2....columnN
FROM table_name;

SQL DISTINCT Clause:


SELECT DISTINCT column1, column2....columnN
FROM table_name;

SQL WHERE Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;

SQL AND/OR Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;

SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);

SQL BETWEEN Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;

SQL LIKE Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };

SQL ORDER BY Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};

SQL GROUP BY Clause:


SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;

SQL COUNT Clause:


SELECT COUNT(column_name)
FROM table_name
WHERE CONDITION;

SQL HAVING Clause:


SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithematic function condition);

SQL CREATE TABLE Statement:


CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

SQL DROP TABLE Statement:


DROP TABLE table_name;

SQL CREATE INDEX Statement :


CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...columnN);

SQL DROP INDEX Statement :


ALTER TABLE table_name
DROP INDEX index_name;

SQL DESC Statement :


DESC table_name;

SQL TRUNCATE TABLE Statement:


TRUNCATE TABLE table_name;

SQL ALTER TABLE Statement:


ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

SQL ALTER TABLE Statement (Rename) :


ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO Statement:


INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);

SQL UPDATE Statement:


UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];

SQL DELETE Statement:


DELETE FROM table_name
WHERE {CONDITION};

SQL CREATE DATABASE Statement:


CREATE DATABASE database_name;

SQL DROP DATABASE Statement:


DROP DATABASE database_name;

SQL USE Statement:


USE database_name;

SQL COMMIT Statement:


COMMIT;

SQL ROLLBACK Statement:


ROLLBACK;

You might also like