CREATE TABLE example_table(
id INT NOT NULL AUTO_INCREMENT, -- NOT NULL constraint with auto-
increment
name VARCHAR(100) NOT NULL, -- NOT NULL constraint
email VARCHAR(255) UNIQUE, -- UNIQUE constraint
age INT CHECK (age >= 18), -- CHECK constraint ensuring age is 18 or above
country_code CHAR(2) DEFAULT 'US', -- DEFAULT constraint
department_id INT, --
PRIMARY KEY (id), -- PRIMARY KEY constraint
INDEX (email) -- INDEX for faster searches
);
CONSTRAINTS
• NOT NULL
• DEFAULT
• UNIQUE
• PRIMARY
• FOREIGN
• CHECK
• INDEX
5.
CREATE TABLE departments(
id INT NOT NULL AUTO_INCREMENT, -- Primary key column
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id) -- Defining the primary key
);
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
department_id INT, -- Column referencing the primary key in 'departments'
PRIMARY KEY (id), -- Primary key of this table
FOREIGN KEY (department_id) REFERENCES departments(id) -- Foreign key constraint
);
6.
ALTER TABLE– ADD/DROP/MODIFYColumn
• Syntax:
ALTER TABLE tablename (ADD/MODIFY/DROP column_name)
• Example
ALTER TABLE Customer
ADD Contact_Phone Char(10);
7.
Example – Contd.,
ALTERTABLE Customer
MODIFY Contact_Phone Char(12);
ALTER TABLE Customer
DROP Contact_Phone;
8.
ALTER TABLE– ADD/DROP/MODIFY Column – Contd.,
• Used to modify the structure of a table by adding and removing columns.
• The ALTER TABLE statement with MODIFY option cannot be used to
change the name of a column or table.
ALTER TABLE—ADD/DROP Constraint–
Contd.,
ALTER TABLE Customer_Account_Details
DROP PRIMARY KEY;
Or
ALTER TABLE Customer_Account_Details
DROP CONSTRAINT Pkey1;
11.
ALTER TABLE—ADD/DROP Constraint–
Contd.,
ALTER TABLE Customer_Transaction
ADD CONSTRAINT Fkey1 FOREIGN KEY (Cust_ID) REFERENCES
Customer_Account_Details (Cust_ID);
ALTER TABLE Customer_Transaction
DROP CONSTRAINT Fkey1;
12.
ALTER TABLE—ADD/DROP Constraint– Contd.,
• A table can have one or more Foreign keys
• Adding a foreign key constraint using ALTER TABLE command will result in
an error if the existing data in master or child table does not support the
foreign key restriction.
13.
ALTER TABLE—ADD/DROP Constraint– Contd.,
• ALTER TABLE statement can be used to Add or Drop primary key
constraint to / from a table
• ALTER TABLE statement can be used to Add or Drop foreign key
constraint to / from a table
• ALTER TABLE statement can be used to Add or Drop Unique constraint
to/ from a table
• ALTER TABLE statement can be used to Add or Drop check constraint
to / from a table
ALTER TABLE—ADD/DROP Constraint–
Contd.,
• If a table already has a primary key,
• then adding a primary key using the ALTER TABLE statement results in an error.
• RDBMS will not allow a PRIMARY KEY constraint (using the ALTER TABLE
statement) on column(s) if the column(s) has NULL or duplicate values
16.
• CREATE TABLEcustomer (
• id INT NOT NULL AUTO_INCREMENT,
• firstname varchar(50) NOT NULL,
• lastname varchar(50) NOT NULL,
• PRIMARY KEY (id)
• ) ENGINE=INNODB;
• CREATE TABLE contact (
• id INT,
• customer_id INT,
• info varchar(50) NOT NULL,
• type varchar(50) NOT NULL
• ) ENGINE=INNODB;
• ALTER TABLE contact ADD INDEX par_ind ( customer_id );
• ALTER TABLE contact ADD CONSTRAINT fk_customer
• FOREIGN KEY ( customer_id ) REFERENCES customer ( id ) ON DELETE CASCADE ON UPDATE RESTRICT;
17.
SQL- DROP TABLE
•DROP TABLE
• Deletes table structure
• Cannot be recovered
• Use with caution
DROP TABLE UnqTable;
SQL- INSERT INTO
•Insert into customer values(‘C4',‘Allan','13-Mar-
09',’Allan1004',’Allan@123');
C4 Allan 13-Mar-09 Allan1004 Allan@123
CustomerId CustomerNAme DateOfRegistration UserId Password
C1 John 1-Mar-09John1001 John@123
C2 Jack 10-Mar-09Jack1002 Jack@123
C3 Bob 12-Mar-09Bob1003 Bob@123
C4 Allan 13-Mar-09Allan1004 Allan@123
21.
SQL- INSERT INTO–Contd.,
• Inserting NULL in to the table-Method1
Insert into customer
values('C5','Simon',NULL,'Symon1005','Symon@123');
C5 Simon NULL Symon1005 Symon@123
22.
SQL- INSERT INTO–Contd.,
• Inserting NULL in to the table-Method2
Insert into customer( CustomerId, CustomerName, UseId, Password)
values( 'C5', 'Simon', 'Symon1005', 'Symon@123');
C5 Simon NULL Symon1005 Symon@123
23.
SQL- DELETE FROM
•With or without WHERE clause
DELETE FROM tablename WHERE condition
• Deleting All Rows
DELETE FROM Customer;
• Deleting Specific Rows
DELETE FROM Customer
WHERE CustomerId = ‘C1’;
24.
Difference Between Deleteand Truncate
DELETE TRUNCATE
Data can be recovered Data cannot be recovered
DML statement DDL statement
DELETE does not release the memory occupied
by the records of the table
TRUNCATE releases the memory occupied by the
records of the table
25.
SQL- UPDATE
• Syntax:
UPDATEtablename SET column_name =value [ WHERE condition]
• Updating All Rows
• Updating Particular rows
UPDATE Customer
SET DateOfReg = NULL;
UPDATE Customer
SET DateOfReg = NULL
Where CustomerId = 'C1';
Retrieving All columnsfrom a table
• To select set of column names,
• Syntax:
SELECT column1, column2,… FROM TableName
• Example:
SELECT *
FROM Customer;
28.
Retrieving Few Columns
•Retrieving only CustomerID and UserId from Customer Table
• Implementing Customized Columns Names
SELECT CustomerId, UserId
FROM Customer;
SELECT CustomerId AS “Customer Identification”,
UserId AS “User Identification”
FROM Customer;
29.
SQL- ALL, DISTINCT
•Get all Customers Name
OR
• Get all distinct Customer Name
SELECT ALL CustomerName
FROM Customer;
SELECT CustomerName
FROM Customer;
SELECT Distinct CustomerName
FROM Customer;
30.
Retrieving Rows basedon Condition
• Syntax
SELECT COL1,COL2,…..
FROM TABLE NAME;
WHERE <SEARCH CONDITION>
31.
Retrieving a subsetof rows (Working of
WHERE Clause)
• Problem Statement: To select CustomerId and UserId of the customer
whose date of registration is 13 March 2009.
CustomerId CustomerName DateOfRegistration UserId Password
C1 John 1-Mar-09 John1001 John@123
C2 Jack 10-Mar-09 Jack1002 Jack@123
C3 Bob 12-Mar-09 Bob1003 Bob@123
C4 Allan 13-Mar-09 Allan1004 Allan@123
C5 Simon Symon1005 Symon@123
32.
Retrieving a subsetof rows (Working of
WHERE Clause) – Contd.,
SELECT CustomerId, UserId FROM Customer
WHERE DateOfReg ='13-Mar-2009';
CustomerI
d UserId
C4 Allan1004
33.
Relational operators
• Listall items whose unit price is > 100
• List the CustomerId and UserId of ‘Allan’
• Relational operators = , < , > , <= , >= , != or < >
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitPrice > 100;
SELECT CustomerId, USerId
FROM Customer
WHERE CustomerName=‘Allan’;
34.
Relational operators –Contd.,
• List all items where discount is at least 10 percent.
SELECT ItemId, ItemName
FROM ITEM
WHERE Discount > 10;
35.
Logical operators
• Listall items where Unit Price is less than 100 and Unit of
measurement is ‘Dozen’.
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitPrice < 100
AND UnitOfMeasurement = ‘Dozen’;
36.
Logical operators –Contd.,
• List all items where either the unit price is less than 100 or Unit of
measurement is ‘Dozen’
• List all items whose Unit Price is not less than 100 .
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitPrice < 100
OR UnitOfMeasurement = ‘Dozen’;
SELECT ItemId, ItemName
FROM ITEM
WHERE NOT UnitPrice < 100;
37.
Retrieval using BETWEEN
•List all Item with Unit Price in the range 100 to 200.
OR
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitPrice >= 100
AND UnitPrice <= 200;
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitPrice
BETWEEN 100 AND 200;
38.
Retrieval using IN
•List all items which have Unit of measurement as ‘Kilogram’ or
’Dozen’.
OR
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitofMeasurement =‘Kilogram’
OR UnitOfMeasurement = ‘Dozen’;
SELECT ItemId, ItemName
FROM ITEM
WHERE UnitOfMeasurement
IN(‘Kilogram’,‘Dozen’);
39.
Retrieval using LIKE
•List all Customers whose name starts with ‘A’ and has ‘l’ as the second
character
OR
SELECT CustomerId,CustomerName
FROM Customer
WHERE CustomerName LIKE ‘Al%’;
SELECT CustomerId,CustomerName
FROM Customer
WHERE CustomerName LIKE _a%’;
40.
Retrieval using ISNULL
• List customers whose date of registration is not available.
OR
SELECT CustomerId,CustomerName
FROM Customer
WHERE DateOfReg IS NULL;
SELECT CustomerId,CustomerName
FROM Customer
WHERE DateOfReg IS NOT NULL;
41.
SQL- Sorting yourresults (ORDER BY)
• List the Items of the retail application in the increasing order of their unit
price
• by default the order is ASCENDING
SELECT ItemId, ItemName
FROM ITEM
ORDER BY UnitPrice;
42.
Retrieval using ORDERBY
• List the items of the retail application in the decreasing order of their
quantity in hand.
SELECT ItemId, ItemName
FROM ITEM
ORDER BY 3 DESC;
43.
Retrieval using ORDERBY
• List the items in their decreasing order of quantity on hand and
increasing order of discount.
SELECT ItemId ,ItemName ,QtyOnHand ,Discount
FROM ITEM
ORDER BY QtyOnHand DESC, Discount;
44.
SELECT statement willretrieve those particular rows where 'country' is the USA.
mysql> SELECT * FROM publisher -> WHERE country='USA';
particular rows where country and city of the publisher are 'USA' and 'New York'.
mysql> SELECT * FROM publisher WHERE country='USA‘ AND pub_city='New
York';
mysql> SELECT pub_name,country,pub_city -> FROM publisher -> WHERE
country='USA' OR pub_city='New York'
SELECT pub_name, country,pub_city FROM publisher ORDER BY pub_name;
ORDER BY pub_name in descending order
mysql> SELECT pub_name,country,pub_city -> FROM publisher -> ORDER BY
pub_name DESC;
SELECT * FROM publisher WHERE name LIKE 'N__e%';
write a SQL query to find the details of those publisher name whose names begin with
‘N’ and the fourth character is ‘e'.