MySQL database Add new fields to table, set primary key, set foreign key, field move location, and summarize how to deploy and maintain after modifying database

Source: Internet
Author: User

1, add a new field for the currently existing table

ALTER TABLE student add studentname varchar (a) not null;

2, set the primary key self-increment for the fields in the currently existing table

ALTER TABLE student add constraint Pk_studentid PrimaryKey (StudentID);

3, set a foreign key for the field in the currently existing table

ALTER TABLE student ADD constraint fk_teacherid_studentinfo foreign key (Teacherid) references Teacherinfo (Teacherid)

ALTER TABLE name add constraint key name foreign key (foreign key field name) references primary table name (primary table primary key name)

4, field move position

ALTER TABLE student Modify StudentID varchar First

Move the position of the StudentID field in the student table to the first bit

5, how to deploy after modifying database structure, and summary of operation process

In the project development process to avoid the need to modify the database table structure, after these days of operation summary of the operation process is

1. Copy the database on the remote server to a local

2. Modify the structure of the database locally and develop new project functions

3, the function is finished, compare the previous database to see what structure has been modified, and then unify these operations using script to write out the way

4, restore the local database to the previous unmodified, and then run the script to modify the structure for the current local database

5, then check to see if the new feature works

6, if the error is repeated 3, 4 steps, until the use of the script can directly modify the database structure, and again test whether the new functionality will work

7, if the function is functioning properly then proceed to the next deployment server

8, first back up the database on the remote server

9, deploy the new features

10. Modify the database in the server using a script

11, after the modification is completed to see if the new feature will work correctly, if it is able to run correctly and there is no error in the log file, it indicates successful deployment!

12, if there is an error, restore the database first and repeat the 8,9,10,11 step

MySQL database Add new fields to table, set primary key, set foreign key, field move location, and summarize how to deploy and maintain after modifying database

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.