0% found this document useful (0 votes)
70 views8 pages

SQL Database Creation and Management Guide

The document provides a comprehensive guide on SQL database operations, including creating, using, and deleting databases and tables. It covers various SQL commands for managing data, such as inserting, updating, and deleting records, as well as modifying table structures. Additionally, it includes examples of queries for retrieving specific data and performing aggregate functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views8 pages

SQL Database Creation and Management Guide

The document provides a comprehensive guide on SQL database operations, including creating, using, and deleting databases and tables. It covers various SQL commands for managing data, such as inserting, updating, and deleting records, as well as modifying table structures. Additionally, it includes examples of queries for retrieving specific data and performing aggregate functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

create database SQL_Batch_3;

create database SQL_Batch_3_DB_1;


create database SQL_Batch_3_DB_2;
create database SQL_Batch_3_DB_11;
create database SQL_Batch_33_DB_1;
create database 123_SQL_Batch_3_DB_1;
create database Sample_1_2_3;
create database Sample_2_3_4;

-- -------------------------------------
show databases;
-- -----------------------------------------
-- this is a comment line
show databases like 'Sample%';
show databases like '%_db';
show databases like '%batch%';

-- Using the database


use test_schema;
use testing;
use temp_1;

-- deleting the database


drop database temp_1;
drop database sql_batch_3_db_2;

drop database sql_batch_3_db_11;


drop database sql_batch_3_db_1;
drop database sql_batch_33_db_1;

drop schema test_schema;


create database test_schema;

-- --------------------------------------------------------------------
-- Database table operations
create database sep_simpli_batch;
use sep_simpli_batch;

-- create table query


/*
create table table_name(
col_1_name data_type constraints(conditions),
col_2_name data_type constraints(conditions),
.
.
.
col_n_name data_type constraints(conditions)
addition constraints);
*/
-- example:1 creating the table for student
create table student(
sid int not null primary key auto_increment,
sname varchar(100) not null,
college varchar(100) not null,
python_mark int);
/*
create ---> new creation
table ---> strructure being created is table
student ---> name of the table
sid ---> column name
int ---> numeric data type (it stores only numbers)
not null ---> constraint (the specified column cannot contain null values)
primary key ---> constraint (it is the unique key column of the table (Key
Attribute))
auto_increment ---> automatically increment the value
sname ---> column name
varchar(100) ---> it is the variable character data type to store the string type
of
data upto 100 characters
college ---> Column name
sql_marks ---> column name
*/

/* */ -- multi line comments


-- single line comment

create table employee(


eid int not null primary key auto_increment,
ename varchar(100) not null,
dob date not null,
fname varchar(100) not null,
mname varchar(100) not null,
company varchar(100) not null,
designation varchar(100) not null,
salary int not null,
email varchar(100) not null,
address varchar(200) not null,
city varchar(100) not null,
state varchar(100) not null,
country varchar(100) not null,
phone bigint);

-- -------------------------------------
create table Cricketer(
cid int not null primary key auto_increment,
cname varchar(100) not null,
dob date not null,
team varchar(100) not null,
matches mediumint not null,
runs int not null,
wickets mediumint not null,
catches mediumint not null,
mom tinyint,
mos tinyint,
icc_trophies tinyint,
ranking tinyint,
phone bigint not null,
email varchar(200) not null);

-- ---------------------------------------
-- View the structure of the table
describe student;
describe employee;
describe cricketer;

desc student;

-- ----------------------------------------------
-- deleting the table
-- 1. drop table : will delete the table data along with the structure
-- 2. truncate table : will delete the table data alone and structure is not
altered

/*
whenever we create a table and add the data into the table, if there comes a
situation where
all the data from the table has to be deleted without deleting the structure of the
table.
then we make use of truncate table command.

whenever we create a table and add the data into the table, if there comes a
situation where
the data along with the table has to be deleted
then we make use of drop table command.
*/

truncate student;
desc student;

drop table cricketer;


desc cricketer;

-- --------------------------------------------------
-- modifying the structure of the table
-- 1. adding new column
-- 2. modifying the size of the datatype of a particular column
-- 3. changing the constraints of the column
-- 4. altering the positions of the columns
-- 5. changing the name of the column
-- 6. deleting a particular column
-- Inorder to modify the structure of the table we make use of alter statements

-- Alter statement to add new columns to the table


-- Version:1 (adding to column to the last position by default)
-- I want to "alter". What you want to alter "table". which table "student".
-- what alteration you want "add column". which column "sql_marks int".
alter table student add column sql_marks int;

-- Version:2 (adding to column to the specified position)


-- I want to "alter". What you want to alter "table". which table "student".
-- what alteration you want "add column". which column "fname varchar(100)" where
to add "after sname".
alter table student add column fname varchar(100) after sname;
alter table student add column mname varchar(100) after fname;
alter table student add column email varchar(150) not null after mname;
desc student;

-- Version:3 (adding multiple columns in a single alter statement)


alter table student
add column address_line varchar(200) not null after email,
add column city varchar(200) not null after address_line,
add column state varchar(200) not null after city,
add column country varchar(200) not null after state,
add column pincode int not null after country;

-- --------------------------------------------------------
-- 2. modifying the size of the datatype of a particular column
-- I want to "alter". What you want to alter "table". which table "student".
-- what alteration you want "modify column". which column "fname" what change
"varchar(150)"
-- Version:1 (Single column change)
alter table student
modify column fname varchar(150);
-- Version:2 (Multiple column change)
alter table student
modify column sname varchar(200) not null,
modify column fname varchar(200) not null,
modify column mname varchar(200) not null,
modify column email varchar(200) not null,
modify column college varchar(200);

desc student;

-- ---------------------------------------------------------------
-- 3. Changing the constraints of the column
alter table student
modify column college varchar(250) not null;

alter table student


modify column sname varchar(200),
modify column fname varchar(200),
modify column mname varchar(200),
modify column email varchar(200),
modify column college varchar(200);

alter table student


modify column email varchar(200) not null unique;

-- ------------------------------------------------------------------------
-- 4. altering the positions of the columns

alter table student


modify column email varchar(200) not null after pincode;

alter table student


modify column email varchar(200) not null after college,
modify column address_line varchar(200) not null after email,
modify column city varchar(200) not null after address_line,
modify column state varchar(200) not null after city,
modify column country varchar(200) not null after state,
modify column pincode varchar(200) not null after country;

-- -------------------------------------------------
-- 5. changing the name of the column
alter table student
rename column sid to stud_id;

desc student;

alter table student


change stud_id student_id int not null;

-- ----------------------------------------------------
-- 6. deleting a particular column

alter table student


drop column python_mark,
drop column sql_marks;

-- ---------------------------------------------------
-- Insert operation on database table
-- Version:1 (Insert query without column names. This is best suited when we are
inserting
-- the data in the same order as in the table)
insert into student values
(1,'Shruthi','Mahesh','Veena','Oxford','shruthi@gmail.com'
,'#1102 4th main','Bangalore','Kar','India',560019);

insert into student values (2,'Robert','Alex','Andria','Oxford','robert7@gmail.com'


,'#11 5th Street','Montrose','Ohio','US',112346);

-- to view the data in the table


select * from student;
-- * indicates all the columns
desc student;
alter table student
modify column student_id int not null auto_increment;
-- -------------------------------------------------------------
-- Version:2 (Insert query with column names. This is best suited when we are
inserting
-- the data in the different order or inserting only specific column
values where other
-- columns are nullable)
insert into student
(sname,fname,mname,college,email,address_line,city,state,country,pincode)
values
('Ahmed','Rafiq','Usma','FNS','mohmad@gmail.com'
,'#21 4th Cross','Maseed Street','Lahore','Pakistan',4321567);

select * from student;

insert into student


(email,address_line,city,state,country,pincode,sname,fname,mname,college)
values
('nidhi@gmail.com','#45 4th
Cross','Ayodhya','UP','India',5546791,'Nidhi','Ram','Sita','PES');

-- version:3
desc student;
insert into student
(email,address_line,city,state,country,pincode)
values
('sam@gmail.com','#45 4th Cross','Ayodhya','UP','India',5546791);
select * from student;
-- -------------------------------------------------------------------------
-- Updating the data in the database table
-- Update command must be very carefully used.
-- If Update command is used without a condition then the changes which was to be
happening on
-- one row will be reflected on all the rows
-- So update must always contains where clause which is used to add the condition.
-- update the name of the student with the id=1 to "Shubham"

set sql_safe_updates=false;
select * from student;
update student set sname='shubham' where student_id=1; -- reflects changes in 1 row
update student set college='RV'; -- reflects the changes in all the rows
update student set student_id=2 where student_id=1;

update student set state='Delhi State' where state='delhi';

desc student;

-- -------------------------------------------------------------------------
-- Deleting the data in the database table
-- Delete command must be very carefully used.
-- If delete command is used without a condition then the changes which was to be
happening on
-- one row will be reflected on all the rows
-- So delete must always contains where clause which is used to add the condition.
-- delete the data of the student with the id=1

set sql_safe_updates=false;
select * from student;
delete from student where student_id=1; -- deletes the data with id=1
delete from student; -- deletes all the data from the table

desc student;
-- --------------------------------------------------------------------------------

-- Query:1 (create table)


CREATE TABLE `sep_simpli_batch`.`employee_tbl` (
`eid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`ename` VARCHAR(45) NOT NULL,
`company` VARCHAR(45) NOT NULL,
`department` VARCHAR(45) NULL,
`designation` VARCHAR(45) NULL,
`salary` INT NULL);

-- Query:2 (adding new columns)


ALTER TABLE `sep_simpli_batch`.`employee_tbl`
ADD COLUMN `dob` DATE NOT NULL AFTER `ename`,
ADD COLUMN `gender` CHAR NOT NULL AFTER `dob`,
ADD COLUMN `experience` INT NOT NULL AFTER `designation`,
ADD COLUMN `address_line` VARCHAR(45) NULL AFTER `salary`,
ADD COLUMN `city` VARCHAR(45) NULL AFTER `address_line`,
ADD COLUMN `state` VARCHAR(45) NULL AFTER `city`,
ADD COLUMN `country` VARCHAR(45) NULL AFTER `state`,
ADD COLUMN `pincode` INT NULL AFTER `country`,
ADD COLUMN `phone` BIGINT NOT NULL AFTER `pincode`,
ADD COLUMN `email` VARCHAR(45) NOT NULL AFTER `phone`,
CHANGE COLUMN `designation` `designation` VARCHAR(45) NOT NULL ,
ADD UNIQUE INDEX `phone_UNIQUE` (`phone` ASC) VISIBLE,
ADD UNIQUE INDEX `email_UNIQUE` (`email` ASC) VISIBLE;
;

Desc employee_tbl;

-- Query: 3
ALTER TABLE `sep_simpli_batch`.`employee_tbl`
CHANGE COLUMN `ename` `ename` VARCHAR(150) NOT NULL ,
CHANGE COLUMN `company` `company` VARCHAR(150) NOT NULL ,
CHANGE COLUMN `department` `department` VARCHAR(150) NULL DEFAULT NULL ,
CHANGE COLUMN `designation` `designation` VARCHAR(150) NOT NULL ,
CHANGE COLUMN `address_line` `address_line` VARCHAR(150) NULL DEFAULT NULL ,
CHANGE COLUMN `city` `city` VARCHAR(150) NULL DEFAULT NULL ,
CHANGE COLUMN `state` `state` VARCHAR(150) NULL DEFAULT NULL ,
CHANGE COLUMN `country` `country` VARCHAR(150) NULL DEFAULT NULL ,
CHANGE COLUMN `email` `email` VARCHAR(150) NOT NULL ;

-- Query:4
INSERT INTO `sep_simpli_batch`.`employee_tbl`
(`ename`, `dob`, `gender`, `company`, `department`, `designation`, `experience`,
`salary`, `address_line`, `city`, `state`, `country`, `pincode`, `phone`, `email`)
VALUES ('Deepesh', '1990-01-01', 'M', 'TCS', 'Dev', 'SE', '5', '60000',
'#46 8th Main', 'Chennai', 'TN', 'India', '789654', '8877665544',
'deepesh@tcs.com');

INSERT INTO `sep_simpli_batch`.`employee_tbl`


(`ename`, `dob`, `gender`, `company`, `department`, `designation`, `experience`,
`salary`, `address_line`, `city`, `state`, `country`, `pincode`, `phone`, `email`)
VALUES
('Prasad', '1992-02-02', 'M', 'Wipro', 'QA', 'TE', '3', '54000',
'#56 9th Cross', 'Mumbai', 'MH', 'India', '678909', '9900665511',
'prasad@wipro.in');

INSERT INTO `sep_simpli_batch`.`employee_tbl`


(`ename`, `dob`, `gender`, `company`, `department`, `designation`, `experience`,
`salary`, `address_line`, `city`, `state`, `country`, `pincode`, `phone`, `email`)
VALUES ('Diksha', '1989-04-05', 'F', 'TCS', 'PMO', 'PM', '7', '78000',
'#2 2nd Cross', 'Bengaluru', 'KAR', 'India', '560019', '87659876512',
'diksha@tcs.com');

-- Query:5 - Write a query to print all the data of the employee_tbl table
select * from employee_tbl;

-- Query:6 - Write a query to print all the data of the employee_tbl table whose id
is 3
select * from employee_tbl where eid=3;

-- Query:7 - Write a query to print all the data of the employee_tbl table who has
the salary more than 70000
select * from employee_tbl where salary > 70000;

-- Query:8 - Write a query to print all the data of the employee_tbl table who has
the salary less than 70000
select * from employee_tbl where salary < 70000;

-- Query:9 - Write a query to print all the data of the employee_tbl table who does
not live in the city bengaluru
select * from employee_tbl where city != 'bengaluru';

-- Query:10 - Write a query to print all the data of the employee_tbl table whose
name starts with D
select * from employee_tbl where ename like 'd%';

-- Query:11 - Write a query to print the names of employee from employee_tbl table
who is not workling in tcs
select ename from employee_tbl where company <> 'TCS';

-- Query:12 - Write a query to print the highest salary from the employee table.
-- aggregate functions (max,min,sum,count,avg)
select max(salary) from employee_tbl;
-- Query:13 - Write a query to print the lowest salary from the employee table.
-- aggregate functions (max,min,sum,count,avg)
select min(salary) from employee_tbl;

-- Query:14 - Write a query to print the average salary from the employee table.
-- aggregate functions (max,min,sum,count,avg)
select avg(salary) from employee_tbl;

You might also like