MySQL Database basic operations

Source: Internet
Author: User

Modify table structure (with Oracle)
    • Rename:
 
   
  
  1. ALTER TABLE test RENAME TO table_1;
    • To add a field:
 
   
  
  1. ALTER TABLE table_1 ADD id int(3);
  2. ALTER TABLE table_1 ADD info varchar(30);
    • Modify Field Name:
 
   
  
  1. ALTER TABLE table_2 change info info2 varchar(30);
    • To modify a field type:
 
   
  
  1. ALTER TABLE table_1 modify info varchar(200);
    • To delete a field:
 
   
  
  1. ALTER TABLE table_1 DROP id;
    • To add a constraint:
 
   
  
  1. ALTER TABLE table_1 ADD CONSTRAINT
Index
    • To create an index:
 
   
  
  1. ALTER TABLE 表名字 ADD INDEX 索引名 (列名);
  2. CREATE INDEX 索引名 ON 表名字 (列名);

* * Note: An index is automatically created in MySQL foreign key * *

    • To view the index of a table
 
   
  
  1. SHOW INDEX FROM table_name;
      *
View
    • Create a View
 
   
  
  1. CREATE OR REPLACE VIEW vw_test[(col1,col2,...)] AS SELECT * FROM table_1;
Import data
 
   
  
  1. LOAD DATA INFILE ‘/tmp/SQL6/in.txt‘ INTO TABLE employee;
Exporting data
 
   
  
  1. SELECT * INTO OUTFILE ‘/tmp/out.txt‘ FROM employee;

Note: The delimiter between the data (field) at this point is a space or a horizontal tab, and if the split symbol between the fields in the imported data is a comma, it is especially noted when importing, and its format is as follows

  
 
  1. LOAD DATA [LOW_PRIORITY] [LOCAL] INFILE ‘file_name.txt‘ [REPLACE | IGNORE]
  2. INTO TABLE tbl_name
  3. [FIELDS
  4. [TERMINATED BY ‘\t‘]
  5. [OPTIONALLY] ENCLOSED BY ‘‘]
  6. [ESCAPED BY ‘\\‘]]
  7. [LINES TERMINATED BY ‘\n‘]
  8. [IGNORE number LINES]
  9. [(col_name,...)]
  10. LOAD DATA INFILE 语句从一个文本文件中以很高的速度读入一个表中。如果指定 LOCAL 关键词,从客户主机读文件。如果 LOCAL 没指定,文件必须位于服务器上。(LOCAL 在 MySQL3.22.6 或以后版本中可用。)
Database backup

(There is also table structure information in addition to the data in the table, which is where the database backup and export differ.)

    • Back up the entire database (at this point in the Linux terminal, not in MySQL, but the MySQL server must be turned on)
 
   
  
  1. mysqldump -u root 数据库名>备份文件名;
    • Backing up the entire table
 
   
  
  1. mysqldump -u root 数据库名 表名称>备份文件名;
    • Code instance
 
   
  
  1. mysqldump -u root mysql_shiyan > db_bak.sql
Database recovery (now in MySQL)
    • Method One
 
   
  
  1. source /tmp/db_bak.sql;(此时位于mysql中)
    • Method Two
      into MySQL:
 
   
  
  1. mysql -u root
  2. create database test;
  3. exit

At this point in the terminal:

 
   
  
  1. mysql -u root test < db_bak.sql

To restore the data in Db_bak.sql to the newly created empty database test



From for notes (Wiz)

MySQL Database basic operations

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.