Show Many times we need to run a SQL script and we do not have graphical tools such as: phpMyAdmin or Adminer so we need to use the command line and use the mysql shell and follow one of the following methods: 1. Execute SQL script in direct way$ mysql -u user -p data-base < script.sql 2. Authenticate and then run SQL script$ mysql -u usr -p data-base Then source script.sql; Or . script.sql; If script.sql file is not found in the current directory, then you must specify the path where it is located Further readings
Spanish video associatedAdministering MySQL from the command line, 10 (15)« Previous PostNext Post »To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. The snapshot is as follows − Now, File -> Open SQL Script to open the SQL script. Alternatively, use the following shortcut key − Ctrl+Shift+O After that an option would be visible for you to choose your .sql file from the disk. In my system, the file is located on the Desktop. I will select the same “tblstudent” SQL file as shown in the following screenshot − After browsing .sql files, you need to select the option “Reconnect to database” as shown in the following screenshot − Now, it will ask for password to connect with MySQL. Add the password and click “OK” as in the following screenshot − Note − Press OK button twice to connect with MySQL. After completing the above process, the following screen will be visible with our SQL file “tblstudent”, which we uploaded before − After that you need to execute the script. To execute the script, click the symbol marked in the following screenshot − After that you will get the following output −
Updated on 30-Jul-2019 22:30:24
To run SQL file in database, you need to use below syntax: mysql -u yourUserName -p yourDatabaseName < yourFileName.sql To understand the above syntax, let us open command prompt using windows+R shortcut key. The snapshot is as follows: After pressing OK button, you will get a command prompt. The snapshot is as follows: Now reach the bin directory, the snapshot is as follows: Here is my file ‘mydb.sql’ which is located in bin directory. The snapshot and content of SQL file is as follows: Now you can type the above syntax which I have discussed to run SQL file. The snapshot of command is as follows: Now you can check the table ‘Instructor’ is created in database test or not. Now switch to database test using USE command. The query is as follows: mysql>Use test Now check the table ‘Instructor’ is present in test database. The query is as follows: Yes, we have Instructor table. Now we need to check the record is present or not which is inserted in the table. The query is as follows: mysql> select *from instructor; The following is the output: +----+-------+ | id | Name | +----+-------+ | 1 | John | | 2 | Larry | | 3 | Sam | +----+-------+ 3 rows in set (0.00 sec)
Updated on 30-Jul-2019 22:30:25
How do I run a .SQL File in shell?sql file from PUTTY, we first connect to DB using syntax <sqlplus> and then DB credentials. Then, we will give the file execution command as <@file_name. sql> and once the file is executed, we will give <exit> to come out of db and then remaining process.
How do I run a SQL script in MySQL?To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. Now, File -> Open SQL Script to open the SQL script. Note − Press OK button twice to connect with MySQL.
How do I run a MySQL query in terminal?First in terminal you have to login using your MySQL username and password Eg: mysql -uroot -p then once loged in select your data base using use <yourdatabasename then you can run this command.
How do I run a .SQL File?Run statements from an open file. Open the Files tool window (View | Tool Windows | Files) and double-click an SQL file. ... . Click the statement that you want to execute. ... . Press Ctrl+Enter or select Execute from the context menu.. |