How to modify the field type and name using SQL alter

Source: Internet
Author: User

How to modify the field type and name using SQL alter

Alter table "table_name"
[Change method]

ADD a column: ADD "column 1" "column 1 data type"

Mysql tutorial> create table Employee (
-> Id int,
-> First_name VARCHAR (15 ),
-> Last_name VARCHAR (15 ),
-> Start_date DATE,
-> End_date DATE,
-> Salary FLOAT (8, 2 ),
-> City VARCHAR (10 ),
-> Description VARCHAR (15)
-> );
Query OK, 0 rows affected (0.03 sec)

Mysql>
Mysql>
Mysql> alter table employee
-> CHANGE First_Name FirstName VARCHAR (20 );
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

Mysql>
Mysql> DESCRIBE employee;
+ ------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------------- + ------ + ----- + --------- + ------- +
| Id | int (11) | YES | NULL |
| FirstName | varchar (20) | YES | NULL |
| Last_name | varchar (15) | YES | NULL |
| Start_date | date | YES | NULL |
| End_date | date | YES | NULL |
| Salary | float (8, 2) | YES | NULL |
| City | varchar (10) | YES | NULL |
| Description | varchar (15) | YES | NULL |
+ ------------- + ------ + ----- + --------- + ------- +
8 rows in set (0.01 sec)

Change Field to varchar type

Mysql> alter table employee
-> CHANGE Last_Name VARCHAR (50 );
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

Add an index to a field

Mysql> alter table myTable
-> Add unique (OrderID, ModelID );
Query OK, 0 rows affected (0.06 sec)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.