You are on page 1of 8

1.

Show databases;
2. Create database amisha;
3. Use amisha;
4. Show tables;
5. Create table abc(rollno integer, fanme varchar(20));
6. Desc abc;
CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName
varchar(255), Address varchar(255), City varchar(255) );

 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

CREATE TABLE Person CREATE TABLE Persons CREATE TABLE Person CREATE TABLE Orders
s ( ( s ( (
ID ID int NOT NULL, ID OrderID
int NOT NULL, LastName int NOT NULL, int NOT NULL,
LastName varchar(255) NOT NULL LastName OrderNumber
varchar(255) NOT NU , FirstName varchar(255) NOT NU int NOT NULL,
LL, FirstName varchar(255), LL, FirstName PersonID int,
varchar(255) NOT NU Age int, varchar(255), PRIMARY KEY (Ord
LL, UNIQUE (ID) Age int, erID),
Age int ); PRIMARY KEY (ID FOREIGN KEY (Per
); ) sonID) REFERENCES Pe
ALTER TABLE Persons ); rsons(PersonID)
DROP INDEX UC_Person; );
ALTER TABLE Persons
DROP PRIMARY KEY; ALTER TABLE Orders
DROP FOREIGN KEY FK_
PersonOrder;
CREATE TABLE Person CREATE TABLE Persons CREATE INDEX idx_lastname ON Persons
s ( ( (LastName);
ID ID int NOT NULL,
int NOT NULL, LastName
LastName varchar(255) NOT NULL ALTER TABLE table_name
varchar(255) NOT NU , DROP INDEX index_name;
LL, FirstName
FirstName varchar(255),
varchar(255), Age int,
Age int, City
CHECK (Age>=18) varchar(255) DEFAULT
); 'Sandnes'
);
ALTER TABLE Persons
DROP CONSTRAINT CHK ALTER TABLE Persons
_PersonAge; ALTER City DROP DEFAU
LT;

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 1
ALTER
ALTER TABLE table_name ALTER TABLE Persons
ADD column_name datatype; ADD DateOfBirth date;
ALTER TABLE table_name ALTER TABLE Persons
DROP COLUMN column_name; DROP COLUMN DateOfBirth;
ALTER TABLE table_name ALTER TABLE Persons
ALTER COLUMN column_name datatype; ALTER COLUMN DateOfBirth year;
OR
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
INSERT
INSERT INTO table_name (column1, column2, col INSERT INTO Customers (CustomerName,
umn3, ...) VALUES (value1, value2, value3, ContactName, Address, City, PostalCode,
...); Country)
VALUES ('Cardinal', 'Tom B.
Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
INSERT INTO table_name INSERT INTO Customers (CustomerName,
VALUES (value1, value2, value3, ...); City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norwa
y');
UPDATE
UPDATE table_name UPDATE Customers
SET column1 = value1, column2 = value2, SET ContactName = 'Alfred Schmidt',
... City= 'Frankfurt'
WHERE condition; WHERE CustomerID = 1;

UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
DELETE
DELETE FROM table_name DELETE FROM Customers
WHERE condition; WHERE CustomerName='Alfreds Futterkiste';
DELETE FROM table_name; DELETE * FROM table_name;

SELECT
SELECT column1, column2, ... SELECT CustomerName, City FROM Customers;
FROM table_name;
SELECT * FROM table_name; SELECT * FROM Customers;

DISTNCT
The SELECT DISTINCT statement SELECT DISTINCT Country FROM Customers;
is used to return only distinct
(different) values. OR
SELECT COUNT(DISTINCT Country) FROM Customers;
SELECT DISTINCT column1, column2, OR
... FROM table_name; SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers);

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 2
WHERE
The WHERE clause is used to filter records. SELECT * FROM Customers
SELECT column1, column2, ... WHERE Country='Mexico';
FROM table_name
WHERE condition; SELECT * FROM Customers
WHERE CustomerID=1;
Operator Description

= Equal

<> Not equal. Note: In some versions of SQL this operator may be written as !=

> 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

SELECT column_name(s) SELECT * FROM Products


FROM table_name WHERE Price BETWEEN 10 AND 20;
WHERE column_name BETWEEN value1 AND v
alue2; SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;

SELECT * FROM Products


WHERE ProductName BETWEEN 'Carnarvon
Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;

SELECT * FROM Orders


WHERE OrderDate BETWEEN #07/04/1996# AND #07/0
9/1996#;

SELECT column_name(s) SELECT * FROM Customers


FROM table_name WHERE Country IN ('Germany', 'France', 'UK');
WHERE column_name IN (value1, value2,
...); SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK
');

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 3
The SQL LIKE Operator SQL Wildcard Characters
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

There are two wildcards used in conjunction with the LIKE operator:

 % - The percent sign represents zero, one, or multiple characters


 _ - The underscore represents a single character

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;
LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"

WHERE CustomerName LIKE '%a' Finds any values that ends with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends with "o"

SQL statement selects all customers with a statement selects all customers with a
CustomerName starting with "a": CustomerName ending with "a":
SELECT * FROM Customers SELECT * FROM Customers
WHERE CustomerName LIKE 'a%'; WHERE CustomerName LIKE '%a';
statement selects all customers with a statement selects all customers with a
CustomerName that have "or" in any CustomerName that have "r" in the second
position: position:
SELECT * FROM Customers SELECT * FROM Customers
WHERE CustomerName LIKE '%or%'; WHERE CustomerName LIKE '_r%';
statement selects all customers with a SQL statement selects all customers with a
CustomerName that starts with "a" and are ContactName that starts with "a" and ends
at least 3 characters in length: with "o":
SELECT * FROM Customers SELECT * FROM Customers
WHERE CustomerName LIKE 'a_%_%'; WHERE ContactName LIKE 'a%o';

statement selects all customers with a SELECT * FROM Customers


CustomerName that does NOT start with WHERE CustomerName NOT LIKE 'a%';
"a":

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 4
SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.

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.
 The OR operator displays a record if any of the conditions separated by OR is TRUE.

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

SELECT column1, column2, ... SELECT column1, column SELECT column1, column2, ...
FROM table_name 2, ... FROM table_name
WHERE condition1 AND conditi FROM table_name WHERE NOT condition;
on2 AND condition3 ...; WHERE condition1 OR co
ndition2 OR condition3
...;
SELECT * FROM Customers SELECT * FROM Customer SELECT * FROM Customers
WHERE Country='Germany' AND s WHERE NOT Country='Germany';
City='Berlin'; WHERE City='Berlin' OR
City='München';

Combining AND, OR and NOT


You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex
expressions):

statement selects all fields from "Customers" where country is NOT "Germany" and NOT
"USA":
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 5
Aggregate functions
SQL MIN() and MAX() Functions
SELECT MIN(column_name) SELECT MIN(Price) AS SmallestPrice
FROM table_name FROM Products;
WHERE condition;
SELECT MAX(column_name) SELECT MAX(Price) AS LargestPrice
FROM table_name FROM Products;
WHERE condition;

COUNT(), AVG() and SUM() Functions


SELECT COUNT(column_name) SELECT COUNT(ProductID)
FROM table_name FROM Products;
WHERE condition;
SELECT AVG(column_name) SELECT AVG(Price)
FROM table_name FROM Products;
WHERE condition;
SELECT SUM(column_name) SELECT SUM(Quantity)
FROM table_name FROM OrderDetails;
WHERE condition;

ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
SELECT * FROM Customers SELECT * FROM Customers
ORDER BY Country; ORDER BY Country DESC;
SELECT * FROM Customers statement selects all customers from the
ORDER BY Country, CustomerName; "Customers" table, sorted by the "Country"
and the "CustomerName" column:
SELECT * FROM Customers statement selects all customers from the
ORDER BY Country ASC, CustomerName DESC; "Customers" table, sorted ascending by the
"Country" and descending by the
"CustomerName" column:
GROUP BY Statement

The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN,
SUM, AVG) to group the result-set by one or more columns.

SELECT column_name(s) SELECT COUNT(CustomerID), Country


FROM table_name FROM Customers
WHERE condition GROUP BY Country;
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 6
SQL Date Data Types
MySQL comes with the following data types for storing a date or a date/time value in the
database:

 DATE - format YYYY-MM-DD


 DATETIME - format: YYYY-MM-DD HH:MI:SS
 TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
 YEAR - format YYYY or YY

SQL Delete Statement


The DELETE Statement is used to delete rows from a table.

Syntax of a SQL DELETE Statement


DELETE FROM table_name [WHERE condition];
 table_name -- the table name which has to be updated.
NOTE: The WHERE clause in the sql delete command is optional and it identifies the rows in
the column that gets deleted. If you do not include the WHERE clause all the rows in the table
is deleted, so be careful while writing a DELETE query without WHERE clause.

SQL DELETE Example


To delete an employee with id 100 from the employee table, the sql delete query would be
like,

DELETE FROM employee WHERE id = 100;

To delete all the rows from the employee table, the query would be like,

DELETE FROM employee;

SQL TRUNCATE Statement


The SQL TRUNCATE command is used to delete all the rows from the table and free the space
containing the table.

Syntax to TRUNCATE a table:

TRUNCATE TABLE table_name;

SQL TRUNCATE Statement Example


To delete all the rows from employee table, the query would be like,

TRUNCATE TABLE employee;


Difference between DELETE and TRUNCATE Statements:

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 7
DELETE Statement: This command deletes only the rows from the table based on the
condition given in the where clause or deletes all the rows from the table if no condition is
specified. But it does not free the space containing the table.
TRUNCATE statement: This command is used to delete all the rows from the table and free
the space containing the table.
SQL DROP Statement:
The SQL DROP command is used to remove an object from the database. If you drop a table,
all the rows in the table is deleted and the table structure is removed from the database.
Once a table is dropped we cannot get it back, so be careful while using DROP command.
When a table is dropped all the references to the table will not be valid.
Syntax to drop a sql table structure:
DROP TABLE table_name;

SQL DROP Statement Example


To drop the table employee, the query would be like

DROP TABLE employee;


Difference between DROP and TRUNCATE Statement:
If a table is dropped, all the relationships with other tables will no longer be valid, the
integrity constraints will be dropped, grant or access privileges on the table will also be
dropped, if you want use the table again it has to be recreated with the integrity constraints,
access privileges and the relationships with other tables should be established again. But, if a
table is truncated, the table structure remains the same, therefore any of the above problems
will not exist.

AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 8

You might also like