r/SQL • • 2d ago

MySQL Need help

So I have this school "project" if you can even call it that where I have created an er model in mysql workbench, and forward engineered and spat out the SQL code. The next step is to start writing queries for the db. However for the life of me I can't figure out how to do that because if I open the SQL code file and open a query tab and write the query it just doesn't let me execute it(the button is grey). I tried asking the teacher but they don't seem to care that I don't know what to do. I tried watching yt guides and even after following the steps perfectly it still leads to the same result. I tried creating a connection but it says not connected to server or smth like that. I apologise for any grammatical errors as English is not my first language. Any help is appreciated.

1 Upvotes

7 comments sorted by

2

u/Discordbothost 2d ago

Check that MySQL Server is installed and running, then open its connection from Workbench's home screen. Workbench can draw models offline, but queries need a server. Try SELECT 1; once connected. If that works, run the generated schema script on your practice database before querying its tables.

1

u/reditandfirgetit 2d ago

Are you connected to the database? I think that could be why it's grayed out

1

u/reditandfirgetit 2d ago

Open a terminal windows, try this

mysql -h hostname -u username -p

If local, use 127.0.0.1 for hostname

1

u/Dangerous_Cup3607 1d ago

Feels like you want to start cooking but you did not have any success turning the stove to the on position. So you might want to read the textbook or project guide on how to establish a successful database connection first. As the sql file didnt know where it should connect to fire up the backend

1

u/Greedy_Ad_7658 1d ago

The grey execute button usually means the query tab has no connection attached. A .sql file opened from the model view is just a text file, and the model itself never touches a server. Two separate pieces are needed:

  1. A running MySQL server. Workbench is only a client. If you haven't installed MySQL Server (the MySQL Installer on Windows, or a package or Docker container elsewhere), there is nothing to connect to, which matches your "not connected to server" message. Your school may also give you a host name instead.
  2. A connection saved on the Workbench home screen. For a local server the host is 127.0.0.1, port 3306, user root and the password you chose at install. Click Test Connection first. If that fails, the problem is the server or the password, not your SQL.

Once the connection opens, open a new SQL tab from that connection (not the file's own tab), then use File > Open SQL Script, or paste the generated script. Run the whole script with the lightning bolt button. That creates the schema and tables. After that, double-click the schema in the left panel so it shows in bold, or start your queries with USE your_schema_name; and the bolt will run them.

If you want, you can also forward engineer straight from the model: Database > Forward Engineer, pick your connection, and Workbench runs the script for you.