Professional Documents
Culture Documents
-- 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
*/
-- 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;
(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
/*
= : 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);
-- 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;
-- 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;
-- delete :
delete from table_name;
delete from table_name where condition;
*/