How to run the script in mysql. Taking running niuzi. SQL as an example, mysqlniuzi. SQL
Suppose you want to run the Script: F: \ hello world \ niuzi. SQL
Method 1:
In the command line (Database not connected), enter mysql-h localhost-u root-p <F: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path !!).
Method 2:
In the command line (the database is connected, and the prompt is mysql>), enter source F: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path) or \. f: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path)
How does mysql command line tool execute script files?
Method 1:
In the command line (Database not connected), enter mysql-h localhost-u root-p123456 <F: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path !!) Press enter.
Method 2:
In the command line (the database is connected, and the prompt is mysql>), enter source F: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path) or \. f: \ hello world \ niuzi. SQL (note that no quotation marks are required for the path). Press enter.
How to execute a mysql SQL script file
The basic steps are as follows: 1. Create an SQL script file that contains SQL commands. The file contains a series of SQL statements. Each statement ends with a; and the file name is assumed to be ss. SQL, file content example (use "--" to comment): -- create a table create table name (Guid Varchar (38) not null primary key, Title Varchar (255 ),) TYPE = InnoDB; -- add the Statusalter table A add Status TinyInt default '0' in table A; -- create index XX_TaskId_1 on A (Id _) on table _); -- Insert into A (Id, ParentId, Name) values (, 'name'); -- after adding, modifying, and deleting data in Table, you may need to submit the transaction Commit. 2. Execute the SQL script file method 1. Run the cmd command (take windows as an example, unix or linux run in a similar console) in the format: [Mysql bin directory] \ mysql-u user name-p password-D database <[full name of the SQL script file path] example: D: \ mysql \ bin \ mysql-uroot-p123456-Dtest <d: \ test \ ss. SQL Note: 1. If the use database is used in the SQL script file, the-D database option can be ignored. 2. If the bin directory of Mysql contains spaces, you need to use "" to include, for example, "C: \ Program Files \ mysql \ bin \ mysql "-u user name-p password-D database <[full name of the SQL script file path] 3. If you need to output the execution result to the file, the following mode can be used: D: \ mysql \ bin \ mysql-uroot-p123456-Dtest <d: \ test \ ss. SQL> d: \ dd.txt method 2 enter the mysql console, run the source command 1, enter the mysql console D: \ mysql \ bin \ mysql-uroot-p123456-Dtest2. Execute the SQL script file