0% found this document useful (0 votes)
15 views8 pages

Alter and Modify

The document discusses various ways to alter tables in MySQL using the ALTER TABLE statement, including adding, modifying, dropping, and renaming columns, as well as renaming tables. The ALTER TABLE statement allows modifying existing table schemas by adding or dropping columns, modifying column definitions, and renaming columns or the entire table. Examples are provided for each operation.

Uploaded by

charul sahu
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)
15 views8 pages

Alter and Modify

The document discusses various ways to alter tables in MySQL using the ALTER TABLE statement, including adding, modifying, dropping, and renaming columns, as well as renaming tables. The ALTER TABLE statement allows modifying existing table schemas by adding or dropping columns, modifying column definitions, and renaming columns or the entire table. Examples are provided for each operation.

Uploaded by

charul sahu
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/ 8

ADD a column in the table

Syntax:

1. ALTER TABLE table_name


2. ADD new_column_name column_definition
3. [ FIRST | AFTER column_name ];

Parameters
table_name: It specifies the name of the table that you want to modify.

new_column_name: It specifies the name of the new column that you want to add
to the table.

column_definition: It specifies the data type and definition of the column (NULL or
NOT NULL, etc).

FIRST | AFTER column_name: It is optional. It tells MySQL where in the table to


create the column. If this parameter is not specified, the new column will be added to
the end of the table.

Example:

In this example, we add a new column "cus_age" in the existing table "cus_tbl".

Use the following query to do this:

1. ALTER TABLE cus_tbl


2. ADD cus_age varchar(40) NOT NULL;

Output:
See the recently added column:

1. SELECT* FROM cus_tbl;

Output:

2) Add multiple columns in the table


Syntax:

1. ALTER TABLE table_name


2. ADD new_column_name column_definition
3. [ FIRST | AFTER column_name ],
4. ADD new_column_name column_definition
5. [ FIRST | AFTER column_name ],
6. ...
7. ;

Example:

In this example, we add two new columns "cus_address", and cus_salary in the
existing table "cus_tbl". cus_address is added after cus_surname column and
cus_salary is added after cus_age column.

Use the following query to do this:

1. ALTER TABLE cus_tbl


2. ADD cus_address varchar(100) NOT NULL
3. AFTER cus_surname,
4. ADD cus_salary int(100) NOT NULL
5. AFTER cus_age ;

See the recently added columns:

1. SELECT* FROM cus_tbl;


3) MODIFY column in the table
The MODIFY command is used to change the column definition of the table.

Syntax:

1. ALTER TABLE table_name


2. MODIFY column_name column_definition
3. [ FIRST | AFTER column_name ];

Example:

In this example, we modify the column cus_surname to be a data type of varchar(50)


and force the column to allow NULL values.

Use the following query to do this:

1. ALTER TABLE cus_tbl


2. MODIFY cus_surname varchar(50) NULL;
See the table structure:

4) DROP column in table


Syntax:

1. ALTER TABLE table_name


2. DROP COLUMN column_name;

Let's take an example to drop the column name "cus_address" from the table
"cus_tbl".
Use the following query to do this:

1. ALTER TABLE cus_tbl


2. DROP COLUMN cus_address;

Output:

See the table structure:

5) RENAME column in table


Syntax:

1. ALTER TABLE table_name


2. CHANGE COLUMN old_name new_name
3. column_definition
4. [ FIRST | AFTER column_name ]

Example:

In this example, we will change the column name "cus_surname" to "cus_title".

Use the following query to do this:

1. ALTER TABLE cus_tbl


2. CHANGE COLUMN cus_surname cus_title
3. varchar(20) NOT NULL;

Output:

6) RENAME table
Syntax:

1. ALTER TABLE table_name


2. RENAME TO new_table_name;
Example:

In this example, the table name cus_tbl is renamed as cus_table.

1. ALTER TABLE cus_tbl


2. RENAME TO cus_table;

Output:

You might also like