SQL Amisha
SQL Amisha
SQL Amisha
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) );
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 !=
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:
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';
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.
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';
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;
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
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.
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:
To delete all the rows from the employee table, the query would be like,
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;
AMISHA DALAL--------- COMPUTER SCIENCE PGT (SQL COMMANDS SUMMARY NOTES) Page 8