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
- /*************************************** *********************
- # Author: chisj
- # Date: 2015.7.22
- # Describe: CreateDatabase 'smartcare' Table 'sct _ Province'
- **************************************** *********************/
- USESmartCare;
- DROPTABLEIFEXISTS 'sct _ Province ';
- CREATETABLE 'sct _ Province '(
- 'Provinceid' int (11) unsignedNOTNULLAUTO_INCREMENT,
- 'Provincecode' varchar (11) DEFAULTNULL,
- 'Parentid' varchar (11) DEFAULTNULL,
- 'Vincename' varchar (50) DEFAULTNULL,
- 'Level' tinyint (1) DEFAULTNULL,
- PRIMARYKEY ('primaryid ')
- ) 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
- #! /Bin/bash
- # Author: chisj
- # Time: 2015.7.22
- # Describe: CreateDatabaseTable
- # Theusernameofmysqldatabase
- USER = "root"
- # Thepasswordofmysqldatabase
- PASS = "dragonwake"
- # Thedatebasenamewillbecreated
- DATABASE = "SmartCare"
- LOCATION =$ {PWD}
- CREATE_TABLE_SCT = create_table_sct _
- Fortable_namein 'ls $ {LOCATION}/$ {CREATE_TABLE_SCT }*'
- Do
- Mysql-u $ USER-p $ PASS <EOF>/dev/null
- SOURCE $ {table_name };
- EOF
- If [$? -Eq0]; then
- Echo "CreateTable $ {table_name} Success! "
- Fi
- 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.