Please open the menu to show more

Thursday, July 27, 2017

some basic sql statements used in database access

to create a database inside MySQL server =>

syntax:
create database database-name;

example,
create database books;
============================================================
to show all the databases =>

syntax:
show databases;

example,
show databases;
============================================================
to delete a database =>

syntax:
drop database-name;

example,
drop books;
============================================================
to delete a table from the database =>


syntax:
drop table tablename;

example,
drop table book;
============================================================
to make database as a default database =>


syntax:
use database-name;

example,
use books;

note: in order to use database it is mandatory to use it first
============================================================
to create a table inside the database =>

syntax:
create table tablename (column-name datatype,column-name datatype,primary key(column-name));

example,
create table book (id INT,topic varchar(30),author varchar(30),price float,publisher varchar(30),primary key(id));

int is used to store numeric values without decimal part like 123
float is used to store numeric values with decimal part like 123.45
varchar is used to store alpha-numeric values like strings
primary key is used on id column, so id cant be dupliate and cant be null
============================================================
to store a record inside table =>

syntax:
insert into tablename values(values inside single quotes seperated by comma);

example, [lets store a record inside book table]
insert into book values('1','OS','Galvin','450.0','TMH');
============================================================
to update records inside a table =>

syntax:
update tablename set column-name='value' where any-condition;

example, [lets change the price of book to 700.0 whose id is 1]
update book set price='700.0' where id='1';
============================================================
to delete records from a table =>

syntax:
delete from tablename; [note: it will delete all the records from table]

example,
delete from book; [all the records from table book will be deleted]

syntax:
delete from tablename where condition; [to delete particular records from table]

example,
delete from book where topic='OS'; [it will delete all the records from book
whose topic is 'OS']
============================================================
to fetch records from the table =>

syntax-1 :
select * from tablename; [it will fetch all the data of table]

example,
select * from book; [all the data of table book will be fetched]

syntax-2 :
select column-name-1,column-name-2 from tablename; [it will fetch all 
the data of table related to given columns only]

example,
select id,topic,publisher from book; [all the data related to id, topic, 
publisher will be fetched from table book]

syntax-3 :
select * from book where condition;

example,
select * from book where price>=200.0 and price<=300.0; [it will fetch all the
data of table book where price range is between 200 and 300.0]

syntax-4 :
select * from tablename order by asc or desc; [get all the data from 
table and sort it either in ascending order or descending]
asc means ascending, desc means descending

example,
select * from book order by price asc; [get all data from table book in ascending order]
select * from book order by price desc; [get all data from table book in ascending order]
============================================================
for more information about the sql statements please go through this link https://www.w3schools.com/sql/

No comments:

Post a Comment