![]() ![]() Output: + -+-+-+ĥ rows in set ( 0.00 sec) Code language: SQL (Structured Query Language) ( sql ) Executing an SQL file using the source command Third, retrieve data from the products table: select * from products Code language: SQL (Structured Query Language) ( sql ) Second, show all the tables in the sales database: + -+ġ row in set ( 0.00 sec) Code language: SQL (Structured Query Language) ( sql ) To verify the execution you can follow these steps:įirst, connect to the sales database: mysql -u root -p -D sales Code language: SQL (Structured Query Language) ( sql ) To execute the script.sql file, you use the following command: mysql -u root -p sales < c:/temp/script.sql Code language: SQL (Structured Query Language) ( sql ) ( 'Gaming Console XYZ', 399.99) Code language: SQL (Structured Query Language) ( sql ) INSERT INTO products (product_name, price) Third, exit the mysql: exit Code language: SQL (Structured Query Language) ( sql )Ĭreate a new file called script.sql with the following code and save it to the c:\temp directory: CREATE TABLE products( Second, create a new database called sales: CREATE DATABASE sales Code language: SQL (Structured Query Language) ( sql ) We’ll create a new database called sales for the demonstration.įirst, connect to the MySQL server: mysql -u root -p Code language: SQL (Structured Query Language) ( sql ) After providing the password, you press the Enter key to execute. The command will prompt you to enter a password for the username. In this command, you replace the username with your MySQL username, database_name with the target database and path/to/your/sqlfile.sql with the actual path to your SQL file. Second, run the following command: mysql -u username -p database_name < path/to/your/sqlfile.sql Code language: SQL (Structured Query Language) ( sql ) Executing a SQL file using MySQL command-line interfaceįirst, open the Command Prompt on Windows or the Terminal on macOS or Linux. ![]() MySQL offers serval ways to execute an SQL file. Running an SQL file in MySQL is an essential skill for database administrators and developers. To import our saved books.sql file created earlier, we’d use many of the same flags and much the same syntax.Summary: in this tutorial, you will learn various ways to execute an SQL file in MySQL using the mysql command line interface, the source command, and the MySQL Workbench. In most cases, importing is just a matter of passing virtually identical options to the mysqlimport command. Now that you’ve learned how to export a backup of a MySQL database, we’ll explore how to reverse the process and import the backup into an existing database.Īs you might imagine, to compliment the mysqldump command used for exporting, there is a similar mysqlimport command for importing. If you need the ability to export (and later recreate) one more more databases, read up on the -databases flag in the official documentation. Instead, by default, only actual tables (and their respective data) are saved and thus will be prepared for later import using this file. sql suffix (which is completely optional but advisable) in the appropriate directory.īy default, mysqldump will not save commands which attempt to modify the existence of the actual database. $ mysqldump -u book_admin -p books > ~/backup/database/books.sqlĪfter entering our password when prompted above, this command then creates our backup file with a.
0 Comments
Leave a Reply. |