SQL - Structured Query Language
Used for CRUD operations in Data Base
c - Create
R - Read
U - Update
D - Delete
DBMS is used to perform these operations
Data Base Management System
Queries - request or command are used to modify the data in the Data Base
Data Base - 2 types
1) Relational Database - Stored in Table format
Each table is related with one another
Relationship established using KEYS
2) Non Relational Database - MongoDB, Firebase, No SQL
KEYWORDS:
They are not case sensitive
Semi colon is optional for every line
SHOW database- to display the avail database
CREATE database - to create a new database
USE - To use or work in the particular database
DROP database - To delete a database
alter database read only = 1 -> The databse is in read only mode.. we cannot
delete(drop) it
alter database read only = 0 -> The databse is not in read only mode.. we can
delete(drop) it4
*** To create a table the alter database read only to be in 0 ***
create table employees ( --> To create a table named employees
emp_id int, --> employee id in integer
emp_name varchar(50), --> employee name in characters/string but it is
called as varchar
salary decimal(7,2), --> salary in decimal - 7 represents the number
of digits including the decimal values
Eg - 35000.50 - 3->1, 5->2 , 0->3, 0->4, 0->5
After decimal 5->6, 0->7
join_date --> joining date in the format of date .. we can
use datetime format also
);
select * from employees; -> * means all... to take all the data from the
table
rename table employees to fam; -> To rename the table name from employees to
fam
select * from employees; -> will show error since we have changed the table
name as fam
select * from fam; -> Select all from the table
drop table fam; -> To delete a table
alter table fam --> to alter the table named fam
add phone_num varchar(10); --> to add a column in the table using
varchar not int data type
select * from fam; --> To show all the data from the table
alter table fam
rename column phone_num to num; --> to rename the column name .. number is
a keyword so don't use it
alter table fam
modify column email varchar(100); --> To modify the column details like..
from 50 to 100 characters or we can even
the data type
first; --> To move the column to the first ..
it's given under the modify statement
after emp_name --> To move the column after emp_name
insert into fam --> To insert
data in the table named fam
values (1, "Madhu" , 30000.50, "2026-07-15", "1234567890"); --> Data - ID ,
name , salary , joining date , phone number
select * from fam; --> To select all from the table
select emp_name,salary from fam; --> To select the particular columns from
the table
select * from fam where emp_id = 1; --> To select a particular data from
the table where emp_id is 1...
select * from fam where emp_id != 1; --> To select the data from the table
where the emp_id is not 1..
select * from fam where salary > 10000.50; --> To select the data from the
table where the salary is greater than 1000
****Should not use = symbol for null****
select * from fam where join_date is null; To select date from the table
where the join)date is null
select * from fam where join_date is not null; To select date from the table
where the join)date is not null
set sql_safe_updates = 0; --> to disable safe update mode
***** What is Safe Update Mode?
When safe update mode is ON (SQL_SAFE_UPDATES = 1), MySQL prevents you from
running dangerous UPDATE or DELETE statements that could affect many rows
accidentally. *****
update fam ]
set join_date = "2025-06-07" ] --> To update the joining date where the
emp_id is 4
where emp_id = 4; ]
Before this we disable the safe update mode for now instead of using primary
keys
update fam
set salary = 50000.70,
join_date = "1950-03-09",
num = "9876543210"
where emp_id = 59213;
** Multiple updates are possible***
delete from fam
where emp_name = "Madhu"; --> To delete data from the table where the
emp_name is Madhu
set autocommit --> SET AUTOCOMMIT is used to control whether MySQL saves
changes automatically after each SQL statement
rollback --> used to undo changes that have not committed yet
create table timestamps ( ]
doj date, ]
dateandtime datetime, ] --> Create a new table named as timestamps
neram time ]
); ]
select * from timestamps;
insert into timestamps
values(current_date(), now(), current_time()) --> current_date() -> gives
present time
now() -> to retrieve
both current date and time
current_time() -> too
retrieve the current time
create table products ( ]
p_id int, ]
p_name varchar(30) unique, ] --> Create a table named products
price decimal (6,2) ]
); ]
p_name varchar(30) unique, --> We use unique here for maintaining or giving
unique names for the products
alter table products --> To modify the existing table
add constraint unique (p_name); --> To add constraint as unique for
product_name
p_name varchar(30) not null, --> doesn't allow the column to have any
null values
alter table products
modify p_name varchar(30) not null; --> To use not null we cannot use add
constraints... we should use modify keyword for it
create table products ( ]
p_id int, ]
p_name varchar(30) not null, ] --> TO create a new table
price decimal (6,2) ]
constraint check_price check(price