Use the MySQL command to modify the table and related table Structures

Source: Internet
Author: User
In practice, we sometimes encounter deletion and modification of database tables and databases. The following articles provide solutions to this problem, that is, use the MySQL command to correctly modify some tables and modify the table structure. Use the MySQL command to modify the table and the table structure: 1. Add a column, as shown in my

In practice, we sometimes encounter deletion and modification of database tables and databases. The following articles provide solutions to this problem, that is, use the MySQL command to correctly modify some tables and modify the table structure. Use the MySQL command to modify the table and the table structure: 1. Add a column, as shown in my

In practice, we sometimes encounter deletion and modification of database tables and databases. The following articles provide solutions to this problem, that is, use the MySQL command to correctly modify some tables and modify the table structure.

Use the MySQL command to modify the table and its structure:

1. Add a column:

For example, in the mytable table in the previous example, adding a column to indicate whether the table is single or not:

 
 
  1. MySQL> alter table mytable add column single char(1);

2. Modify records
Modify the single record of abccs to "y ":

 
 
  1. MySQL> update mytable set single='y' where name='abccs';

Now let's see what happened:

 
 
  1. MySQL> select * from mytable;
  2. +----------+------+------------+-----------+--------+
  3. | name | sex | birth | birthaddr | single |
  4. +----------+------+------------+-----------+--------+
  5. | abccs|f | 1977-07-07 | china | y |
  6. | mary |f | 1978-12-12 | usa | NULL |
  7. | tom |m | 1970-09-02 | usa | NULL |
  8. +----------+------+------------+-----------+--------+

3. Add records

We have already discussed how to add a record to repeat this record for ease of viewing:

 
 
  1. MySQL> insert into mytable
  2. -> values ('abc','f','1966-08-17','china','n');
  3. Query OK, 1 row affected (0.05 sec)

Check it out:

 
 
  1. MySQL> select * from mytable;
  2. +----------+------+------------+-----------+--------+
  3. | name | sex | birth | birthaddr | single |
  4. +----------+------+------------+-----------+--------+
  5. | abccs|f | 1977-07-07 | china | y |
  6. | mary |f | 1978-12-12 | usa | NULL |
  7. | tom |m | 1970-09-02 | usa | NULL |
  8. | abc |f | 1966-08-17 | china | n |
  9. +----------+------+------------+-----------+--------+

3. delete records

Run the following MySQL command to delete a record in the table:

 
 
  1. MySQL> delete from mytable where name='abc';

DELETE: DELETE a record from the table that meets the conditions given by where.

The result is displayed as follows:

 
 
  1. MySQL> select * from mytable;
  2. +----------+------+------------+-----------+--------+
  3. | name | sex | birth | birthaddr | single |
  4. +----------+------+------------+-----------+--------+
  5. | abccs|f | 1977-07-07 | china | y |
  6. | mary |f | 1978-12-12 | usa | NULL |
  7. | tom |m | 1970-09-02 | usa | NULL |
  8. +----------+------+------------+-----------+--------+

4. delete a table:

MySQL> drop table ***** (name of table 1), and *** name of table 2;

You can delete one or more tables with caution.

5. delete a database:

MySQL> drop database name;

Use it with caution.

6. Database Backup:

Return to DOS:

 
 
  1. MySQL> quit
  2. d:MySQLbin

Use the following MySQL command to back up the database abccs:

 
 
  1. MySQLdump --opt abccs>abccs.dbb

Abccs. dbb is the backup file of your database abccs.

7. Use MySQL in batches:

First, create a batch file mytest. SQL with the following content:

 
 
  1. use abccs;
  2. select * from mytable;
  3. select name,sex from mytable where name='abccs';

Run the following command in DOS:

 
 
  1. d:MySQLbin MySQL < mytest.sql

The execution result is displayed on the screen.

If you want to view the results, but there are many output results, you can use the following MySQL command:

 
 
  1. MySQL < mytest.sql | more

We can also output the result to a file:

 
 
  1. MySQL < mytest.sql > mytest.out

Add index 549830479

 
 
  1. MySQL> alter table tablename change depno int (5) not null;
  2. MySQL> alter table tablename add index name (field name 1 [, field name 2…]);
  3. MySQL> alter table tablename add index emp_name (name );

Index with primary keyword 549830479

 
 
  1. MySQL> alter table tablename add primary key(id);

Add an index of 549830479 with unique restrictions

 
 
  1. MySQL> alter table tablename add unique emp_name2(cardnumber);

Delete An index 549830479

 
 
  1. MySQL>alter table tablename drop index emp_name;

Modify Table: 549830479

Add field: 549830479

 
 
  1. MySQL> ALTER TABLE table_name ADD field_name field_type;

Modify the original field name and type: 549830479

 
 
  1. MySQL> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

Delete field: 549830479

 
 
  1. MySQL> ALTER TABLE table_name DROP field_name;

PS; Modify Field attributes

 
 
  1. ALTER TABLE tableName MODIFY cloumnName DATETIME;

The above content is an introduction to table modification and table structure modification using the MySQL command. I hope you will get some benefits.

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.