Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute multiple SQL queries in MySQL Workbench?

I am using MySQL Workbench CE for Windows version 5.2.40.

I want to execute the following SQL queries together. However I can only execute the SQL queries by first executing the CREATE TABLE query, and then executing the INSERT INTO query and after that executing the SELECT query.

CREATE TABLE testTable(
    Name VARCHAR(20),
    Address VARCHAR(50),
    Gender VARCHAR(10)
)

INSERT INTO testTable
    VALUES
    ('Derp', 'ForeverAlone Street', 'Male'),
    ('Derpina', 'Whiterun Breezehome', 'Female')

Select * FROM testTable

So how do I execute the CREATE TABLE, INSERT INTO and the SELECT queries by one click?

like image 795
user921020 Avatar asked Jul 23 '12 17:07

user921020


People also ask

How do I run multiple SQL queries in MySQL Workbench?

How to run Multiple SQL Queries in MySQL Workbench explains how you can run multiple statements in single query window by separating them with semicolon ; You can have different types of statements such as delete, update or insert in single query window and all can be executed with single click.

Can I execute multiple queries in MySQL?

MySQL also supports the execution of a string containing multiple statements separated by semicolon ( ; ) characters. This capability is enabled by special options that are specified either when you connect to the server with mysql_real_connect() or after connecting by calling mysql_set_server_option() .

How do I run a SQL query in MySQL Workbench?

To do that, first select the desired database from the left column menu by double-clicking it. Then type in the MySQL query you want to run in the text field in the middle of the program window and use the yellow lightning button above that text field to run the query.

How do I run a SQL script in a workbench?

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.


2 Answers

You could use Ctrl+Shift+Enter to run everything with semicolon end.

For Mac +shift+return

like image 200
Weijing Jay Lin Avatar answered Sep 17 '22 19:09

Weijing Jay Lin


Add a semicolon after each statement:

CREATE TABLE testTable(     Name VARCHAR(20),     Address VARCHAR(50),     Gender VARCHAR(10) );  INSERT INTO testTable VALUES ('Derp', 'ForeverAlone Street', 'Male'), ('Derpina', 'Whiterun Breezehome', 'Female');  SELECT * FROM testTable; 
like image 36
bfavaretto Avatar answered Sep 17 '22 19:09

bfavaretto