0% found this document useful (0 votes)
29 views4 pages

SQL

The document provides SQL commands for creating databases and tables, inserting and selecting data from tables, updating records, and altering table structures. It includes commands to show databases, use a database, select all records from a table, create tables, add columns, modify columns, and define primary and foreign keys.

Uploaded by

Kanti Chaurasia
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)
29 views4 pages

SQL

The document provides SQL commands for creating databases and tables, inserting and selecting data from tables, updating records, and altering table structures. It includes commands to show databases, use a database, select all records from a table, create tables, add columns, modify columns, and define primary and foreign keys.

Uploaded by

Kanti Chaurasia
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
You are on page 1/ 4

-show databases;

-use hrm
-select * from emp;

________________________________________________________________
# To display databases in the mysql
--> show databases ;
# To create the new databases in mysql
--> Create database database_name ;
eg.
--> create database hrm;
hrm is the name of the database.
# To open database or use database
--> use database_name;
eg.
--> use hrm;
Now hrm database is open and we can create table in the database hrm.
# To check list of tables
--> show tables;
To create the table
--> create table table_name
( col_name1 data_type constraint,
col name2 data_type constraint,
.......);
eg.
create table dept (
dept_id int ,
dept_name char(25),
location char(10)
);

# To view the schema/structure of the table describe table_name ; or desc


table_name;
eg.
describe dept;
or desc dept;
-----------------------------------------------------------------------------------
-----------
# To add the values or record in the table using insert command syntax :
Insert into table_name values (col_name1, col_name2, col_name3 , .....)
values (val1 , val2 , val3 , ...);
or
insert into table_name values (val1 , val2 , val3 , ....);
eg.
insert into dept (dept_id,dept_name,location)
values (1,'Sales','Mumbai');
or
insert into dept values (1,'sales','Mumbai');
insert into dept values
---------------------------------------------------------
#To display records of the tables
syntax :
select *

---------------------------------------------------------
#To remove or delete the table from the database
syntax :
drop table table_name ;

Not Null Constraint :


By default, a column can hold NULL values,
The NOT NULL constraint enforces a column to
NOT accept NULL values,
This enforces a field to always contain a value,
which means that you cannot insert a new record, or update

#create table dept (


dept_id int,dept_name char(25) Not NULL,
location char(10) default ' Mumbai',
Primary key (dept_id)
);

#insert into dept values (2,'production','Delhi'):

Q> Create a table employee


employee id
employee name
designation
Basic salary
department id

sales = sales manager and junior assistant


research = marketing head and marketing
manager, suppervisor

Solution==>
#create table emp (
emp_id int Primary Key,
emp_name char(20) not null,
designation char(20) not null,
basic_salary numeric(10,2) not null,
dept_id int
);

#insert into emp values(1001, 'Ramesh', 'Sales Manager', 45000,1);


insert into emp values(1002, 'Ganesh', 'Junior Assistant', 15000,1);
insert into emp values(1003, 'Prathmesh', 'Manager', 55000,1);
insert into emp values(1004, 'Sandesh', 'Supervisor', 25000,1);
insert into emp values(1005, 'Dinesh', 'System Analyst', 52000,1);
insert into emp values(1006, 'Rudra', 'Junior Assistant', 45000,1);
insert into emp values(1007, 'Vilas', 'Marketing Head', 45000,1);
insert into emp values(1008, 'Mahesh', 'Marketing Manager', 65000,1);

#TO VIEW ==> select * from emp; {* means all columns are selected}

# To view only id, name and designation ==> select emp_id,emp_name, designation
from emp,
# select dept_id, emp_name, basic_salary from emp;

# select emp_name as Employee_Name, basic_salary as Salary from emp;


((Alias is used for changing the value of heading in column))

((where clause is used to apply condition on the select command,


Syntax:
select * from table_name where field_name=value
#select * from emp where basic_salary >40000;

# we use "or" for displaying two values.

# select * from emp where not dept_id = 4;

Q> display salary of the manager?

Q> person having salary more than 20000 and less than 40000 but not equal to?
==> basic_salary > 20000 and basic_salary < 40000

# select * from emp where basic_salary between 20000 and 40000; ==. it will show
both the values
to view all data add "not " before between
# select distinct designation from emp;
# select max(basic_salary) from emp; {{to show maximum salary}}
# select min(basic_salary) from emp; {{to show minimum salary}}
# select sum(basic_salary) from emp; {{to show sum of salary}}
# select count (emp_id) from emp;

# Update command
It is used to modify the records of the table.
syntax:
Update table_name set field_name = value, field_name = value, ....
where...
eg.
update

# update emp set emp_name = 'Ganesh' where emp_id = 1002;


select * from emp;

Q> how to increase salary by 5000?


ans==>
update emp set basic_salary = basic_salary + 5000
--> where designation = 'Junior Assistant';

# drop table_name; (to remove the table which will be not recovered)

# primary key
we cant enter the duplicate value in it.
# default key:
if we dont enter the value it will automatically displY THE default value.

# To add the column in the table


syntax :
alter table table_name add
column_name datatype contraint;
eg:
alter table employee add dept_id int not null;
........................................................
# To modify the column of the table,
syntax:
alter table table_name modify
column_name datatype contraint;
eg,
alter table employee modify design char(25) not null;
.......................
# To remove the column from the table
syntax :
alter table table_name drop column_name;
eg,
alter table employee drop dept_id;
...................................................
# Foreign key:
It is to create the relationship between two tables.
Primary key of one table will be foreign key of another table.
Syntax:
Foreign Key (field_name) references table_name(field name)
Eg.
create table dept(
dept_id int primary key,
dept_name char(20) not null,
location char(20)
);
create table emp(
emp_id int primary key,
emp_name char(20) not null,
desig char(20) not null,
salary numeric(10,2),
dept_id int, foreign key (dept_id) references dept(dept_id)
);
# alter table employee add
dept_id int foreign key references dept

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy