Create database tables in batches using Shell in CentOS

Source: Internet
Author: User

Create database tables in batches using Shell in CentOS

This article continues to explore the combination of Shell and MySQL. I don't know how to quickly create a database table and add the corresponding basic data after a database is designed, I currently know how to use Shell and SQL scripts to achieve my goal-Quickly, multiple times, and reusable database tables.

Create an SQL script for a database table:

[Cpp] view plaincopy
  1. /*************************************** *********************
  2. # Author: chisj
  3. # Date: 2015.7.22
  4. # Describe: CreateDatabase 'smartcare' Table 'sct _ Province'
  5. **************************************** *********************/
  6. USESmartCare;
  7. DROPTABLEIFEXISTS 'sct _ Province ';
  8. CREATETABLE 'sct _ Province '(
  9. 'Provinceid' int (11) unsignedNOTNULLAUTO_INCREMENT,
  10. 'Provincecode' varchar (11) DEFAULTNULL,
  11. 'Parentid' varchar (11) DEFAULTNULL,
  12. 'Vincename' varchar (50) DEFAULTNULL,
  13. 'Level' tinyint (1) DEFAULTNULL,
  14. PRIMARYKEY ('primaryid ')
  15. ) ENGINE = MyISAMAUTO_INCREMENT = 1 DEFAULTCHARSET = utf8;


Execute Shell scripts for all SQL scripts (Multiple SQL scripts in the specified directory) in the specified directory: 

[Plain] view plaincopy
  1. #! /Bin/bash
  2. # Author: chisj
  3. # Time: 2015.7.22
  4. # Describe: CreateDatabaseTable
  5. # Theusernameofmysqldatabase
  6. USER = "root"
  7. # Thepasswordofmysqldatabase
  8. PASS = "dragonwake"
  9. # Thedatebasenamewillbecreated
  10. DATABASE = "SmartCare"
  11. LOCATION =$ {PWD}
  12. CREATE_TABLE_SCT = create_table_sct _
  13. Fortable_namein 'ls $ {LOCATION}/$ {CREATE_TABLE_SCT }*'
  14. Do
  15. Mysql-u $ USER-p $ PASS <EOF>/dev/null
  16. SOURCE $ {table_name };
  17. EOF
  18. If [$? -Eq0]; then
  19. Echo "CreateTable $ {table_name} Success! "
  20. Fi
  21. Done
In fact, there are many ways to create data tables in batches using Shell. I have seen it before. Unfortunately, I did not remember 0.0. It is obvious that records are very important.

So I made it again according to my ideas: First, it is necessary to write the SQL script for creating a data table, and write all the data into one (I personally think it is not good ), you can also write an SQL script according to a table (this method is currently used in our city). Then, traverse the SQL script name in the specified directory and find an SQL script for creating a database table, then run the command once.

It also provides the CSDN: Shell to create a MySQL DATA table.

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.