Query about using SQL created database from a beginner

Ammy Khan 1 Reputation point
2022-07-12T09:15:03.173+00:00

Hey, I created database and named it "mysql_learning" but now to add data to it I need to select database as the default database. But when i tried to 'USE' the database, it is giving me a nasty error that I have an error in my SQL syntax. Need help how to fix this error so that I can continue to learn SQL, attaching the screenshot of my MySQL Workbench for further illustrating my query. An insight will be helpful in this regard for keeping my learning, thanks in advance for the support.219806-screenshot-47.png

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,794 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bjoern Peters 8,856 Reputation points
    2022-07-12T11:31:47.833+00:00

    You are using a mySQL workbench, so I am expecting you are running a mySQL database.

    If I am connecting to my mySQL database (using HeideSQL), I have to "terminate" every statement with a ;

    So you are getting an error on using "USE mysql_learning" because your statement (the line above) was not terminated...

    create database mysql_learning;
    USE mysql_learning;
    CREATE TABLE demo (
    id INT NULL,
    firstname VARCHAR(50) NULL DEFAULT NULL,
    lastname VARCHAR(50) NULL DEFAULT NULL
    );

    1 person found this answer helpful.
    0 comments No comments

  2. Olaf Helper 43,246 Reputation points
    2022-07-12T09:21:57.177+00:00

    But when i tried to 'USE' the database

    USE is a very specific command for SQL Server tools like SSMS or SqlCmd; but even there it isn't a valid T-SQL statement, only a tool command.
    See https://learn.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go?view=sql-server-ver16

    AFAIK MySQL works similar to Oracle and there an instance is a database and versa vice; no need to switch a context to a specific database, remove the GO command and go aon.