You are on page 1of 5

/*

database: collection of data


dbms : database management system
goal of dbms :
to store and manage the data
read /retrieve the data
rdbms : relational database management system -- stores data in the form
of tables
mysql: rdbms
sql : structured query language
types of commands in SQL:
DDL: create,alter,drop,truncate
DML: insert,update,delete
DCL: grant ,revoke (dba)
TCL: commit,rollback,savepoint
DQL: select
*/

-- databases >> database >> tables >> rows and columns

-- DDL :
-- CREATE
-- create database db_name;
-- db_aug
create database db_aug;
show databases;
use db_aug;

-- create table :
-- create table table_name(columns datatype,columnN datatype);
/*
emp : table
empid, empname,salary, loc
*/

create table db_aug.emp(empid int,empname varchar(20),salary int,loc


varchar(25)); -- table definiton

-- varchar(size): alphanumeric values ,variable length,neha


-- char(size) : alphanumeric values , fixed length ,neha

-- ALTER
/*
emp : table
empid, empname,salary, loc , contact
*/

-- add column
-- rename column
-- modify datatype and size
-- remove column
-- add constraint

-- 1. add column
-- alter table table_name ADD COLUMN column_difinition;
alter table emp ADD COLUMN contact int;
alter table emp ADD COLUMN row_id int FIRST;
alter table emp ADD COLUMN gender char(1) AFTER empname;

-- describe table
-- desc table_name;
desc emp;

-- modify column
-- alter table table_name MODIFY COLUMN new_column_definition;
alter table emp MODIFY COLUMN contact varchar(10);
alter table emp MODIFY COLUMN loc varchar(20);

-- rename column
-- alter table table_name rename column old_column_name to
new_column_name;
alter table emp rename column contact to mob_no;
alter table emp rename to emp_data;
desc emp_data;

-- remove column
-- alter table table_name DROP COLUMN column_name;
alter table emp_data DROP COLUMN mob_no;
alter table emp_data DROP COLUMN row_id,DROP COLUMN gender;

-- TRUNCATE
-- truncate table table_name;
select * from db_emp.emp_data;
truncate table db_emp.emp_data;
desc db_emp.emp_data;

-- DROP
-- drop table table_name;
drop table db_emp.emp_data;
drop table emp_data;
drop database db_aug;

-- CRUD: create data,read,update,delete


-- DML+ DQL :
-- INSERT
create database stud_info;
use stud_info;
/*
student
roll_no,stud_name,gender,cgpa,branch
*/
create table student (roll_no int,stud_name varchar(20),gender
char(1),cgpa int,branch varchar(25));

-- insert into table_name(column names) values (set of values)


-- 101, gaurav,M,7,mech
insert into student (roll_no,stud_name,gender,cgpa,branch) values
(101,'gaurav','M',7,'mech');
insert into student values (102,'gauravi','F',null,'comp');
insert into student (roll_no,stud_name,gender,branch)values
(103,'rahul','M','entc');
insert into student values (104,'dipak','M',8,'mech'),

(105,'dipika','F',5,'comp');

-- read data
-- select * from table_name;
-- * represents all columns
-- from : used to specify a data source
select * from student; -- all rows and all columns
select roll_no,cgpa from student; -- specific columns ,all rows

-- where : filter out the data in result set


select * from student where roll_no=104;

/*
= : equal to
!= : not equal to
> : greater than
< : less than
<= : less than equal to
>= : greater than equal to
between : specify a range of values
in : specify multiple possible values for a column
is null : used to check for null values on a column
like : specify a pattern
*/

select * from student where cgpa between 5 and 8; -- start and end value
included
select * from student where cgpa not between 5 and 8;
select * from student where roll_no in (101,103,108);
select * from student where roll_no not in (101,103,108);

select * from student where roll_no IS NULL;


select * from student where roll_no IS NOT NULL;
select * from student where cgpa IS NULL;

-- SPECIFIC COLUMNS OF SPECIFIC ROWS


select roll_no,stud_name from student where cgpa IS NULL;

-- like : specify a pattern


-- _ (underscore) : single char
-- % : 0 or more char

select * from student;


select * from student where stud_name like 'd%';
select * from student where stud_name like '%a';
select * from student where branch like '%n%';
select * from student where stud_name like 'g____';

-- regexp
select * from student where branch regexp 'h$';

-- UPDATE
-- update table_name set column_name=new value;
update student set branch='computer science';
update student set branch='mechanical' where gender='M';

-- intership
alter table student ADD COLUMN internship int;
select * from student;

update student set internship=0;

update student set internship=


case roll_no
when 101 then 2
when 102 then 3
when 105 then 1
else 5
end;

-- DELETE
-- delete from table_name;
delete from student; -- deletes all rows
delete from student where roll_no=105;
truncate table student; -- deletes all rows
drop table student;

/*
DDL :
create :
create database db_name;
use db_name;
create table table_name(column name datatypes.......);
alter :
alter table table_name add column,modify column, rename column,drop column
column_definition;
truncate :
truncate table table_name;
drop :
drop database db_name;
drop table table_name;

DML:
insert :
insert into table_name (column names) values (set of values);
insert into table_name (column names) values (set of values),(set of
values);

read :
select * from table_name; -- all columns all rows
select column_names from table_name : -- specific columns all rows
select * from table_name where condition; -- all columns specific rows
select column_names from table_name where condition ; -- specific columns
specific rows

update :
update table_name set column_name=new value;
update table_name set column_name= new value where condition;

update table_name set column_name=


case column_name
when value then new_value
else value
end;

-- delete :
delete from table_name;
delete from table_name where condition;
*/

select * from db_emp.emp;


update db_emp.emp set salary= null where empid=1005;
set sql_safe_updates=0;

You might also like