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

Chapter 14

My notes of computer science.

Uploaded by

m9070827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Chapter 14

My notes of computer science.

Uploaded by

m9070827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

14.

Table creation and data manipulation commands


DATABASE COMMNADS

1. VIEW EXISTING DATABASE


To view existing database names, the command is : SHOW DATABASES ;

2. CREATING DATABASE IN MYSQL


For creating the database in MySQL, we write the following
command : CREATE DATABASE <databasename> ;
e.g.
CREATE DATABASE Student ;
3. ACCESSING DATABASE
For accessing already existing database , we write :
USE <databasename> ;
e.g.
USE Student ;
4. DELETING DATABASE
For deleting any existing database , the command is :
DROP DATABASE <databasename> ;
e.g DROP DATABASE Student ;
5. VIEWING TABLE IN DATABASE
In order to view tables present in currently accessed database , command is : SHOW TABLES ;
DELETING DATA FROM TABLES
The DELETE command removes rows from a table.
For example, to remove the details of those employee from EMPLOYEE table whose grade is A1.
DELETE FROM EMPLOYEE
WHERE GRADE =’A1’ ;

DROPPING TABLES
The DROP TABLE command lets you drop a table from the database.
e.g. to drop a table employee, we need to write :
DROP TABLE employee ;

S.NO. DELETE COMMAND DROP TABLE COMMAND


1 It is a DML command. It is a DDL Command.
2 This command is used to delete only rows This command is used to delete all the data of the table
of data from a table along with the structure of the table. The table is no
longer recognized when this command gets executed.
3 Syntax of DELETE command is: Syntax of DROP command is :
DELETE FROM <tablename> DROP TABLE <tablename> ;
WHERE <condition> ;
ALTER TABLE COMMAND
The ALTER TABLE command is used to change definitions of existing tables.(adding columns,deleting columns
etc.). The ALTER TABLE command is used for :
1. adding columns to a table
2. Modifying column-definitions of a table.
3. Deleting columns of a table.
4. Adding constraints to table.
5. Enabling/Disabling constraints.

ADDING COLUMNS TO TABLE

To add a column to a table, ALTER TABLE command can be used as per following syntax:

ALTER TABLE <tablename>


ADD <Column name> <datatype> <constraint> ;
e.g. to add a new column ADDRESS to the EMPLOYEE table.

ALTER TABLE EMPLOYEE


ADD ADDRESS VARCHAR(50);
Now following commands are given for the table. Predict the table contents after each of the following statements:
(i) ALTER TABLE testt ADD col3 INT ;
(ii) ALTER TABLE testt ADD col4 INT NOT NULL ;
(iii) ALTER TABLE testt ADD col5 CHAR(3) NOT NULL ;
(iv) ALTER TABLE testt ADD col6 VARCHAR(3);

MODIFYING COLUMNS

Column name and data type of column can be changed as per following syntax :

ALTER TABLE <table name>


CHANGE <old column name> <new column name> <new datatype>;
If Only data type of column need to be changed, then

ALTER TABLE <table name>


MODIFY <column name> <new datatype>;
DELETING COLUMNS

To delete a column from a table, the ALTER TABLE command takes the following form :

ALTER TABLE <table name>


DROP <column name>;
e.g. to delete column GRADE from table EMPLOYEE, we will write :
ALTER TABLE EMPLOYEE
DROP GRADE ;

Q: Distinguish between ALTER Command and UPDATE command of SQL.


S.NO. ALTER COMMAND UPDATE COMMAND
1. It is a DDL Command It is a DML command
2. It is used to change the definition of It is used to modify the data values present
existing table, i.e. adding column, in the rows of the table.
deleting column, etc.
3. Syntax for adding column in a table: Syntax for using UPDATE command:
ALTER TABLE <tablename> UPDATE <Tablename>
ADD <Column name><Datatype> ; SET <Columnname>=value
WHERE <Condition> ;

You might also like