DDL Statement in MySQL
How to connect to MySQL?
$mysqlurootp Enterpassword:root
How to display all available Databases?
mysql>SHOWDATABASES;
How to create a new Database?
Syntax:
mysql>CREATEDATABASE<database_name>;
Example:
mysql>CREATEDATABASEdemo;
How to connect to a particular Database?
Syntax:
mysql>USE<database_name>;OR mysql>CONNECT<database_name>;
Example:
mysql>USEdemo;OR mysql>CONNECTdemo;
How to Know to which Database user is connected?
mysql>SELECTDATABASE()FROMDUAL;
How to display all tables available in a Database?
mysql>SHOWTABLES;
How to create a Table?
mysql>CREATETABLEemployee( empidINT, enameVARCHAR(20), salaryFLOAT(10,2));
How to create a Table by copying the structure and data of an existing Table?
mysql>CREATETABLEstudentASSELECT*FROMemployees;
How to create a Table by copying the structure of an existing Table?
mysql> CREATE TABLE department AS SELECT * FROM student WHERE9=8;
How to Describe the structure of a Table?
mysql>DESCRIBEemployee;OR mysql>DESCemployee;
How to add new Column(s) to an existing Table?
mysql>ALTERTABLEemployee ADDaddressVARCHAR(30); mysql>ALTERTABLEemployee ADDcityVARCHAR(70),ADDphoneINT;
How to Modify structure of existing column(s) of existing Table?
mysql>ALTERTABLEemployee MODIFYaddressVARCHAR(40); mysql>ALTERTABLEemployee MODIFYenameVARCHAR(22),MODIFYcityVARCHAR(22);
How to drop existing column(s) of existing Table:
mysql>ALTERTABLEemployee DROPphone; mysql>ALTERTABLEemployee DROPename,DROPsalary;
How to rename existing column(s) of existing Table?
mysql>ALTERTABLEemployee CHANGEaddressnew_addressVARCHAR(60); mysql>ALTERTABLEemployee CHANGE empid id VARCHAR (100), CHANGE new_address address VARCHAR(100);
How to rename an existing Table(s)?
mysql>RENAMETABLEemployeeTOnew_employee; mysql>RENAMETABLE new_employeeTOemployees, studentTOstudents, departmentTOdepartments;
How to drop existing Table?
mysql>DROPTABLEstudents;
How to Truncate an existing Table?
mysql>TRUNCATETABLEemployees;
Data Types in MYSQL
SMALLINT: A small integer. The signed range is -32768 to 32767. The unsigned range is 0 to 65535.
MEDIUMINT: A medium-sized integer. The signed range is -8388608 to 8388607. The unsigned range is 0 to 16777215.
INT: A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
BIGINT:
large
integer.
The
signed
range
is
-9223372036854775808
to
9223372036854775807. The unsigned range is 0 to 18446744073709551615.
FLOAT: A small (single-precision) floating-point number. Permissible values are -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to 3.402823466E+38. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system.
DATE: A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format.
TIME : A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format.
DATETIME: A date and time combination. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. MySQL displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format.
YEAR[(2|4)]: A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the permissible values are 1901 to 2155, and 0000. In two-digit format, the permissible values are 70 to 69, representing years from 1970 to 2069. MySQL displays YEAR values in YYYY format.
TIMESTAMP: A timestamp. The range is '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
The CHAR and VARCHAR Types: The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters. The length of a CHAR column is fixed to the length that you declare when you create the
table. The length can be any value from 0 to 255. When CHAR values are stored, they are rightpadded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed. Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.