Data Definition Statement
In this section we will describe you the syntax of most of the Data Definition statements supported by MySQL. These statements are given below :Creating a Database
For creating a database the command syntax :CREATE DATABASE [IF NOT EXISTS] <db_name>
By the command CREATE DATABASE we can create the database with the given name. But using this command you have the CREATE privilege for the database. And if you are trying to create a database, which already exists and you are not using the IF NOT EXIST option then you get an error. But if you are using this option then you will not get the error but the database is not created again.
Example for Creating a new Database :
mysql> CREATE DATABASE IF NOT EXISTS Employee; |
mysql> CREATE DATABASE Employee; |
mysql> CREATE DATABASE IF NOT EXISTS Employee; |
mysql> show databases; |
Creating a Table
Syntax for creating a table a little more complex than creating a database.CREATE [TEMPORARY] TABLE [IF NOT EXISTS] <table_name>[(create_definition,...)] [table_options] [select_statement]
IF NOT EXISTS option works like the same as it does in CREATE DATABASE statement. But TEMPORARY option is used to define that the table has been created will available only until the current connection to the database is open. When the connection is disconnected even by accidentally, table will be deleted.
There are a variety of options are available that we can put inside the create_definition brackets Example :
<column_name> <data type> [NOT NULL | NULL] [DEFAULT <default_value>][AUTO_INCREMENT] PRIMARY KEY (<index_column_name>,...)
- The column_name is used to define the field or column of the table of each record. And each column name has data type also.
- The NOT NULL/NULL option is used to define that the field is require the data or not.
- The DEFAULT option is used to set the default value (which the database uses in lieu of input data).
- An integer column can have the AUTO-INCREMENT attributes. The AUTO-INCREMENT option is used to automatically set counts up when NULL is input into the column?s field.
- PRIMARY KEY option is used to indicate which column of fields, will be used to form an index for faster access to the table?s records.
mysql> use employee |
mysql> SHOW TABLES; |
Altering the Database
The general syntax for altering the database is :ALTER {DATABASE} [db_name] alter_specification [alter_specification]......
By ALTER DATABASE command you can change the overall characteristics of a database. And these characteristics are stored in db.opt file in the database directory. But for using ALTER DATABASE command you have ALTER privilege on the database. The following example increase the size of one file that available in the database. Example :
mysql> ALTER DATABASE Emp |
Altering the Table
The general syntax for altering the table is :ALTER TABLE tbl_name alter_specification [, alter_specification] ...
By the ALTER TABLE command you can change the structure of an existing table. This commands allows you add, modify or delete the columns, create or destroy the indexes, rename the columns or the table itself. For using the ALTER TABLE command you need the ALTER, INSERT and CREATE privileges for the tables. Here is some examples are given below that helps you to understand the different alter specification for altering the table.
The first example is used to rename the table name by the command ALTER TABLE old_name RENAME TO new_name;
mysql> show tables; |
mysql> DESCRIBE Emp; |
mysql> DESCRIBE Emp; |
mysql> DESCRIBE Emp; |
Dropping the Database
The general syntax for dropping the database command is :DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
DROP DATABASE command is used to drop the all tables in the given database and deletes the database also. But for using the DROP DATABASE statement you need the DROP privilege on the database. If you are not using IF EXISTS option and the database is not available which you want to drop then it occurs the error but if you are using this option then it doesn't occur the error. Example :
mysql> SHOW DATABASES; |
Dropping the Table
The general syntax for dropping the table command is :DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name
DROP TABLE statement is used to remove one or more tables in the database but for this you must have the DROP privilege for each table. By this command all the data of table and the definition also has been removed. If you are not using IF EXISTS option and any table name in the argument list do not exists the MySQL returns an error with the name of non existing tables it was unable to drop. But it drops the all tables of the list that do exist. By using TEMPORARY keyword, the statement drops only temporary tables, the statement does not end an ongoing transaction and it does not check the access right because the temporary table is visible only to the client that creates it, that?s why the access right checking is not is not necessary. If you are not using the TEMPORARY keyword then the DROP TABLE command automatically commits the current active transaction. Example :
mysql> SHOW TABLES; |
Rename the Table
The general syntax of rename the table command is :RENAME TABLE tbl_name TO new_tbl_name, tbl_name2 TO new_tbl_name2 ...
By this statement we can rename the one or more tables. The rename operation is work automatically that means no other thread can access any of the tables while the rename process is running. Example :
mysql> SHOW TABLES; |
No comments:
Post a Comment