![]() The command returns the following output: Query OK, 0 rows affected (0.02 sec) The following source command executes the script2.sql file: source c:/temp/script2.sql Code language: SQL (Structured Query Language) ( sql ) ( 'Eva Davis', ' ') Code language: SQL (Structured Query Language) ( sql ) Insert five customers into the customers table INSERT INTO customers (customer_name, email) VALUES Third, execute the source command: source path/to/your/sqlfile.sql Code language: SQL (Structured Query Language) ( sql )įor example, you can create a new file called script2.sq in the C:\temp directory with the following code: - Create the customers table CREATE TABLE customers ( Second, switch to a target database e.g., sales where you want to execute the SQL statements: use sales Code language: SQL (Structured Query Language) ( sql ) ![]() To execute an SQL file using the source command, you follow these steps:įirst, connect to the MySQL server using the mysql client program: mysql -u root -p Code language: SQL (Structured Query Language) ( sql ) 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. ![]() ![]() 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |