Error in SQL statement, in MySQL Workbench

0

I'm doing two exercises from Workbench . The exercise asks to create a table called CLIENTS with the following fields:

cod : size 5 integer filled with zeros, does not accept nulls.

name : variable text of 30, does not accept nulls.

Last names: Variable text of 50, does not accept nulls.

Balance : number with 2 decimals and a resolution of 6 digits. It will not accept negative numbers. Default value 1000.

This is the query:

CREATE TABLE CLIENTES

cod(5) NOT NULL, PRIMARY KEY

nombre (30) NULL

apellidos (50) NULL

saldo (2,6) NOT NULL default 1000

and shows an error of: error code 1046, no database selected

    
asked by kitkat 30.01.2017 в 17:49
source

3 answers

5

Usually when you start Workbench you have no database selected and that is what throws you

error code 1046, no database selected

In Spanish it would be

error código 1046, no se ha seleccionado base de datos

first select the database to which you want to add the table with the command

USE myDataBase;

Where myDatabase is the name of your database and then execute the statement you already have

    
answered by 30.01.2017 / 17:54
source
2

The error is quite clear, No database selected , you must select a database before proceeding to create a table or execute your querys you must select the database using the following "command / query" :

USE nombre_base_de_datos;

Or, giving double click on the database you want to use on the left side of the client (as you can see, when you give double click it is bold) the name of the database that we are using at the moment):

    
answered by 30.01.2017 в 17:57
2

That syntax is incorrect. From Workbench you can create a table in two ways:

Option 1: With SQL syntax:

CREATE TABLE 'INSETA_EL_NOMBRE_DE_TU_BASE_DE_DATOS'.'clientes' (
  'cod' int(5) unsigned NOT NULL,
  'nombre' varchar(30) NOT NULL,
  'apellidos' varchar(50) NOT NULL,
  'saldo' decimal(6, 2) unsigned NOT NULL,
  PRIMARY KEY ('cod')
);

Option 2: Filling a form from the Workbench graphical interface. It is a simple form that you fill in indicating the type of data of the fields, the name ... and it generates you the corresponding SQL query and executes it. Watch this link

    
answered by 30.01.2017 в 18:06