0% found this document useful (0 votes)
3 views10 pages

SQL COMMANDS

The document provides an overview of SQL commands, including creating, showing, dropping, and using databases, as well as Data Definition Language (DDL) commands like CREATE, ALTER, RENAME, TRUNCATE, and DROP for managing tables. It also covers Data Manipulation Language (DML) commands such as SELECT, INSERT, UPDATE, and DELETE for handling data within those tables. Each command is accompanied by syntax and examples to illustrate its usage.

Uploaded by

m acharya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views10 pages

SQL COMMANDS

The document provides an overview of SQL commands, including creating, showing, dropping, and using databases, as well as Data Definition Language (DDL) commands like CREATE, ALTER, RENAME, TRUNCATE, and DROP for managing tables. It also covers Data Manipulation Language (DML) commands such as SELECT, INSERT, UPDATE, and DELETE for handling data within those tables. Each command is accompanied by syntax and examples to illustrate its usage.

Uploaded by

m acharya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

SQL COMMANDS , SYNTAX & EXAMPLES

1)Creating Database
a)command :CREATE  create an empty database.
b)syntax:
CREATE Database<Database_name>;

c)Example :mysql>CREATE Database college;


2)Showing Database
a)command: SHOW  Show the list of existing database on
the server.
b)syntax:
SHOW <expression>;

c)Example : mysql>SHOW databases;


3)Dropping a database
a)command: DROP Drop the existing database.
b)syntax:
DROP database<database_name>;

c)Example: mysql> DROP database college;


4)Using a Database
a)command: USE Specified database is now current
working database.
b)syntax:
USE <database_name>;
c)Example : mysql>USE college;

DDL COMMANDS
1)CREATE : Create a table (only structure).
Command :CREATE
Syntax:
CREATE TABLE <table_name>
(
Col_name1 datatype(size),
Col_name2 datatype(size),
---------------------------------
Col_nameN datatype(size)
);

Example :
Table name : STUDENT(ROLL,NAME,MARK)
mysql> CREATE TABLE STUDENT
( ROLL NUMBER ,
NAME VARCHAR(15),
MARK NUMBER);
Display Table structure
Command : DESCRIPTION / DESC
Syntax :
DESC <table_name>;
Example : mysql>DESC STUDENT;
STUDENT
Fields Type Null Key Default Extra
ROLL NUMBER YES NULL
NAME VARCHAR(15) YES NULL
MARK NUMBER YES NULL

2) ALTER : Change the existing structure of the


table(add/drop column , modify data type of column, rename
column name).
Command :ALTER
Syntax :
ALTER TABLE <TABLE_NAME > ADD
<NEW_COL>DATATYPE(SIZE);
OR
ALTER TABLE <TABLE_NAME>DROP COLUMN
<COL_NAME>;
OR
ALTER TABLE <TABLE_NAME> MODIFY
<COLUMN_NAME> <NEW DATA TYPE><SIZE>;
OR
ALTER TABLE <TABLE_NAME>RENAME
<EXISTING_COL_NAME>TO<NEW_COL_NAME>;

Example :
A)Add column PHNO. ,datatype Number to Student
table
Mysql>ALTER TABLE STUDENT ADD PHNO
NUMBER;
To display structure : DESC STUDENT;
FIELDS TYPE NULL KEY DEFAULT EXTRA
ROLL NUMBER YES NULL
NAME VARCHAR YES NULL
MARK NUMBER YES NULL
PHNO NUMBER YES NULL

B)Drop column PHNO from Student table


Mysql>ALTER TABLE STUDENT DROP COLUMN
PHNO;
To display structure : DESC STUDENT;
Fields Type Null Key Default Extra
ROLL NUMBER YES NULL
NAME VARCHAR YES NULL
MARK NUMBER YES NULL

C)Change the datatype of MARK(Number) to


MARK(Decimal(5,2))
Mysql>ALTER TABLE STUDENT MODIFY MARK
DECIMAL(5,2);
To display structure : DESC STUDENT;
FIELD TYPE NUL KE DEFAUL EXTR
S L Y T A
ROLL NUMBER YES NULL
NAME VARCHAR YES NULL
MARK DECIMAL(5,2 YES NULL
)

D)Change the name of the column MARK to TOTALMARK


Mysql>ALTER TABLE STUDENT RENAME MARK TO
TOTALMARK;
To display structure : DESC STUDENT;
FIELDS TYPE NUL KE DEFAU EXTR
L Y LT A
ROLL NUMBER YES NULL
NAME VARCHAR YES NULL
TOTALMA DECIMAL(5 YES NULL
RK ,2)

3) RENAME : This command is used to change the name of


the existing table.
Command :RENAME
Syntax :
RENAME <OLD TABLE_NAME> TO <NEW
TABLE_NAME>;

Example: Change the name of the table from STUDENT to


IT_STUDENT
Mysql>RENAME STUDENT TO IT_STUDENT;
To display structure : DESC IT_STUDENT;
FIELDS TYPE NUL KE DEFAU EXTR
L Y LT A
ROLL NUMBER YES NULL
NAME VARCHAR YES NULL
TOTALMA DECIMAL(5 YES NULL
RK ,2)

4)TRUNCATE :This command is used to delete all records


from the table but the structure remains same.
Command : TRUNCATE
Syntax :
TRUNCATE TABLE <TABLE_NAME>;

Example : Delete all records from IT_STUDENT table.


Mysql>TRUNCATE TABLE IT_STUDENT;

5)DROP : This command is used to permanently delete the


table from database.
Command : DROP
Syntax :
DROP TABLE <TABLE_NAME>;

Example : Drop IT_STUDENT table


Mysql>DROP TABLE IT_STUDENT;
To display structure : DESC IT_STUDENT;
No table exist
DML COMMANDS
1)SELECT : This command is used to fetch the data from
database and project to the user.
Command :SELECT

Syntax : SELECT <clo1> , <col2> , <col3> , -------<col N>


FROM <table_name>; (Selected fields)
OR
SELECT * FROM <table_name>; (Display all records)

Example : SELECT * FROM STUDENT ;


2)INSERT : Insert records in a table.
Command : INSERT
Syntax :
INSERT INTO <TABLE_NAME>
VALUES (value1,value2,------,value N);
OR

INSERT INTO <TABLE_NAME>


(col1, col2, -----------------,col N)
VALUES(value1,value2, ------value N);

Example : student
Roll Name Mark
1 Raj 65
2 Yash 48
3 Piyush 69
Mysql> INSERT INTO STUDENT VALUES(1, ’Raj’ ,65);
Mysql> INSERT INTO STUDENT VALUES(2, ’Yash’ ,48);
Mysql> INSERT INTO STUDENT VALUES(3,
’Piyush’ ,69);
To display Table
Mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 Raj 65
2 Yash 48
3 Piyush 69

3)UPDATE : This command is used to change values that are


already in table.
Command : UPDATE
Syntax :
UPDATE <TABLE_NAME>
SET <ATTRIBUTE> = <VALUE/EXPRESSION>
WHERE <CONDITION>;
Example : Change the mark of Yash 55
Mysql>UPDATE STUDENT
SET MARK=55
WHERE NAME = ‘Yash’;
To display Table
Mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 Raj 65
2 Yash 55
3 Piyush 69

4)DELETE :This command is used to delete a record form


the table.
Command : DELETE
Syntax :
DELETE FROM <TABLE_NAME>
WHERE <CONDITION>;

Example : Delete the record Piyush from Student table.


Mysql>DELETE FROM STUDENT
WHERE NAME = “Piyush’;
To display Table
Mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 Raj 65
2 Yash 55

You might also like