Create database command will create School database in MySQL.
mysql> CREATE DATABASE [IF NOT EXISTS] Name_of_database;
IF NOT EXISTS is an optional part of this statement which prevents you from an error if there exists a database with the given name in the database catalog.
To open an existing database, USE command is used.
mysql> USE school ;
Example:
mysql> use school;
Database changed
SHOW DATABASES;
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| abc |
| hopital |
| ipclass |
| kdma |
| mysql |
| products |
| school |
| test |
+——————–+
9 rows in set (0.00 sec)
SHOW TABLES;
mysql> use ipclass;
Database changed
mysql> show tables;
+——————-+
| Tables_in_ipclass |
+——————-+
| cabhub |
| customer |
| ip12 |
| posting |
+——————-+
4 rows in set (0.15 sec)
To delete database:
mysql> DROP DATABASE School;
To delete Table:
mysql> DROP TABLE Student;
mysql> DESCRIBE Student;
mysql> desc customer;
+——-+———-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+———-+——+—–+———+——-+
| ccode | int(2) | YES | | NULL | |
| cname | char(20) | YES | | NULL | |
| vcode | int(3) | YES | | NULL | |
+——-+———-+——+—–+———+——-+
3 rows in set (0.05 sec)
Some DDL statements are as follows
The CREATE statement is used to create a table in a database. In this command, we need to give information about table like number of columns, rows and its types and constraints.
The data type specifies what type of data, the column can hold and the size or constraint is optional.
e.g. If we want to create a table PERSONS that contains five columns: P_Id, FirstName, LastName, Address and City.
We use the following
Getting listings of database and tables
mysql>SHOW TABLES;
mysql>SHOW DATABASES;
DESCRIBE or DESC command is used to verify the structure of a table that you have created.
Syntax DESCRIBE ; or DESC ;
For example, DESC SCHOOL;
The DROP statement is used to remove the table definition and all data, constraints and permission specified for that table.
Syntax DROP TABLE table_name;
e.g. DROP TABLE EMPLOYEE;
The ALTER statement is used to add, delete or modify columns and constraints in the existing table.
Syntax
ALTER TABLE table_name ADD column_name data_type;
e.g. ALTER TABLE STUDENT ADD Section CHAR;
The above query will add column Section to STUDENT table, whose data type is character.
Syntax ALTER TABLE table_name DROP COLUMN column_name;
e.g. ALTER TABLE STUDENT DROP COLUMN location;
The above query will delete a column location from STUDENT table.
Syntax ALTER TABLE table_name MODIFY column_name data_type;
e.g. ALTER TABLE STUDENT MODIFY Fee NUMBER(15,2); The above query will modify the data type of Fee column, of STUDENT table.
Syntax
A L T E R T A B L E t a b l e _ n a m e D R O P
C o n s t r a i n t _ N a m e ;
e.g. ALTER TABLE STUDENT DROP Primary Key;
The above query will delete the primary key constraint from STUDENT table.
The RENAME statement is used to rename a table.
Syntax
RENAME old_table_name TO new_table_name;
e.g. RENAME STUDENT TO CANDIDATE;
The above query will rename STUDENT table to CANDIDATE
