• Introduction • Inserting Data into Table
• Some MySQL Elements • Making simple select query
• Databases in MySQL • More DDL Command
• Creating Table
1. Literals
2. Data types
Numeric Data type
Date & Time
String/text Types
3. Null Values
4. Comments
Literals
1. Literals
- Refers to a fixed data value
- Data value can be text or number
- Character / Numeric:
- To store apostrophe in a text literal use \’
- Number Literal:
- Integer literal : eg: 10,20,30
- Real / float literal: eg: 25.5, 20.0,33.456
Data Types
NULL
• NULL is not Zero
• Use NULL when the actual value is not known
Comment
Create database <database_name>;
Use <database_name>;
Show databases;
Show tables;
create table table_name (column_name datatype(size),
column_name datatype(size), column_name datatype(size)) ;
Type 1 INSERT INTO TABLE_NAME VALUES (VALUE1, VALUE2….);
Type 2 INSERT INTO TABLE (COLUMN_NAME1, COLUMN NAME2…)
VALUES (VALUE1, VALUE2… );
Create table table_name ( column 1_name Dtype <NOT NULL>,
column 2_name Dtype <DEFAULT>,
column 3_name Dtype <UNIQUE>,
column 4_name Dtype <CHECK>,
column 5_name Dtype <PRIMARY KEY>,
column 6_name Dtype <FOREIGN KEY>,)
UNIQUE
Example 1:
Create table student (roll_no integer UNIQUE,
F_Name char(20) NOT NULL,
L_Name char (20) );
NOT NULL
Example 1:
Create table student (roll_no integer NOT NULL,
F_Name char(20) NOT NULL,
L_Name char (20) );
DEFAULT
Example 1:
create table student (roll_no integer UNIQUE,
F_Name char(20) NOT NULL,
L_Name char (20),
gender char(10) DEFAULT ‘Male’ | ‘Female’);
Example 2:
create table student (roll_no integer UNIQUE,
F_Name char(20) NOT NULL,
L_Name char (20),
score integer DEFAULT 80);
CHECK
Example 1:
Create table student (roll_no integer UNIQUE,
F_Name char(20) NOT NULL,
L_Name char (20),
Age integer CHECK (Age > 18));
Note: MySQL ignores CHECK constraints internally. Ie: CHECK constraint get
passed but IGNORED.
PRIMARY KEY
Example 1: Example 2:
Create table student Create table student
(roll_no integer Primary Key, (roll_no integer,
F_Name char(20) NOT NULL, F_Name char(20) NOT NULL,
L_Name char (20) L_Name char (20)
Age integer); Age integer,
PRIMARY KEY (roll_no));
Example 3: Defining primary key through ALTER Table Command
Alter table student ADD (Primary Key (roll_no) );
FOREIGN KEY
Table: Customer Table: Orders
Column_Name Characteristic Column_Name Characteristic
CID Primary Key Order_ID Primary Key
F_Name Order_Date
L_Name Customer_CID Foreign Key
Amount
Example 1:
Create table Orders
(Order_ID integer, Order_Date Date, Customer_CID integer,
Amount integer, Primary Key (Order_ID), Foreign Key Customer_CID
reference Customer (CID));
FOREIGN KEY
Example 1:
Create table Orders
(Order_ID integer, Order_Date Date, Customer_CID integer,
Amount integer, Primary Key (Order_ID), Foreign Key Customer_CID
reference Customer (CID));
Select Commands
Select All Data ( * ) Performing simple calculation.
Select particular columns Condition based on range ( BETWEEN)
Select particular row based on certain condition Condition based on LIST (in | not in)
using (WHERE )
Condition based on pattern match (percent (%) |
Eliminating redundant data (with keyword DISTINCT) underscore ( _ ) ) using LIKE keyword
Selecting from all the rows – ALL keyword
Searching for NULL values.
Viewing structure of a Table (DESC)
Select particular columns
sql> select name, roll_no from student;
Sql> select emp_no, emp_name, designation from employee;
Select All Data ( * )
Sql> select * from student;
Sql> select * from employee;
Select particular row based on certain condition using (WHERE )
Sql> select * from student where class_sec=‘12B’;
Sql> select * from employee where designation= ‘Manager’;
Eliminating redundant data (with keyword DISTINCT)
Sql> select distinct class_sec from student;
Sql> select distinct city from student;
Selecting from all the rows – ALL keyword
Sql> select all city from student;
Sql> select all class_sec from student;
Viewing structure of a Table (DESC)
Sql> desc employee;
Sql> describe student;
Performing simple calculation.
Sql> select 2 * 4; or Sql> select curdate ();
Condition based on range ( BETWEEN)
Sql> select * from student where total between 100 and 200;
Sql> select * from employee where salary between 30000 and 50000;
Condition based on LIST (in | not in)
Sql> select * from student where city in (‘Blore’, ‘Chennai’, ‘Mumbai’)
Sql> select * from student where city not in ((‘Blore’, ‘Chennai’);
Condition based on pattern match (percent (%) |
underscore ( _ ) ) using LIKE keyword
Sql> select f_name, l_name, city from employee where pin_code like
’56%’;
Sql> select f_name, l_name, city from employee where city like
’Ch%’;
Sql> select f_name, l_name, city from employee where city like ‘%e’;
Searching for NULL values.
Sql> select roll_no, name, class_sec from student where city is null;
Sql> select roll_no, name, class_sec from student where city is null;
ORDER BY
Order by Clause
Order by Clause
MySQL Order By clause is used to sort the table data in either
Ascending order or Descending order.
By default, data is not inserted into Tables in any order unless we
have an index. So, If we want to retrieve the data in any particular
order, we have to sort it by using MySQL Order By statement.
Syntax:-
SELECT Column_Names FROM Table_Name
ORDER BY {Column1}[ASC | DESC] {Column2}[ASC | DESC] ;
MySQL Order by– default
Suppose we are having student table with following data
Now we write the query – select * from student order by class;
MySQL Order by– default
Now we write the query – select * from student order by class;
NOTE:
Query result will be in ascending order of class. If we not specify
asc/desc in query then ascending clause is applied by default.
MySQL Order by– desc
Suppose we are having student table with following data
Now we write the
query –
select * from student
order by class desc;
Order by– Multiple Column
Suppose we are having
student tablewith
following data
Now we write the query –
select * from student order by
class asc, marks asc;
Note: Query result will be ascending order of class and if same class exists then ordering will done on
marks column(ascending order
Order by– Multiple Column
Order by– Multiple Column
Order by– Multiple Column
Order by– on the basis of Expression
DDL
Type 1
Change the name Alter table table_name CHANGE old_col_name
of the column new_col_name new_data_type(size);
Type 2 Alter table table_name MODIFY (col_name
To Modify existing new_data_type(size)); [first | After column]
Columns of a Table
Note: Only can change definition.
Type 3
To add a new Alter table table_name ADD (col_name data_type(size) );
columns to a Table
Type 4
Deleting a column Alter table table_name DROP col_name;
from a table
SELECT column_name, AGG_FUNCTION(column_name)
FROM table_name
GROUP BY column_name;
Product Region Sales
Pen East 200
Pen West 150
Pencil East 100
Pencil West 50
SELECT Product, SUM(Sales)
FROM Sales Product SUM(Sales)
GROUP BY Product; Pen 350
Pencil 150
SELECT column_name, AGG_FUNCTION(column_name)
FROM table_name
GROUP BY column_name
HAVING condition;
SELECT column_name, AGG_FUNCTION(column_name)
FROM table_name
GROUP BY column_name
HAVING condition;
SELECT Product, SUM(Sales) Product SUM(Sales)
FROM Sales Pen 350
GROUP BY Product
HAVING SUM(Sales) > 200;
Feature GROUP BY HAVING
Purpose Group data Filter grouped data
Used with Aggregate functions Aggregate functions
Executes Before HAVING After GROUP BY
Example GROUP BY Product HAVING SUM(Sales) > 200
Table: Students
Name Class Marks
Amit 12 85
Ravi 12 78
Neha 11 90
Asha 11 80
Q: Write an SQL query to display the average marks per class where average is greater than
80.
SELECT Class, AVG(Marks) FROM Students GROUP BY Class HAVING AVG(Marks) > 80;
Class AVG(Marks)
11 85.0
Table: Order
OrderID Customer Product Quantity
1 Ramesh Pen 10
2 Suresh Pencil 5
3 Ramesh Pen 15
4 Geeta Pencil 7
5 Suresh Pen 5
Write an SQL query to display each customer's total quantity ordered.
Bonus: Display only those customers who ordered more than 15 items in total.
SELECT Customer, SUM(Quantity) AS TotalQuantity FROM Orders GROUP BY Customer;
SELECT Customer, SUM(Quantity) AS TotalQuantity FROM Orders GROUP BY Customer HAVING
SUM(Quantity) > 15;
DML
Sql> update student set total=300, city=‘Bangalore’ where roll_no =104;
Sql> update employee set bonus =5000 where dept=‘Manager’;
Sql> update employee set bonus =5000 where dept=(‘Manager’ or
‘analyst’;
Delete all values from a relation:
Sql> delete from student;
Sql> delete from employee;
Delete a specific record from a relation based on condition:
Sql> delete from student where roll_no=103;
Sql> delete from employee where salary<12000;
Sql> delete from <table_name> [where <logic>];
Sql> delete from student where roll_no = 104;
Sql> delete from employee where salary >200000;
Sql> drop table [if exist] <table_name>;
Sql> drop table student;
Sql> drop table employee;
Thank You
By Gubert L
Dept of Computer Science