MySQL CREATE TABLE command - mysql

I am new to MySQL commands. please, I tried creating a table using the MAMP MySQL editor and I got an error #1046 No database selected..Below is the simple code:
[code]
CREATE TABLE EMPLOYEE_TABLE AS:
(SSN NUMBER(9) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
FIRST_NAME VARCHAR2(20) NOT NULL,
MIDDLE_NAME VARCHAR2(20) NOT NULL,
ST ADDRESS VARCHARS2(20) NOT NULL,
CITY CHAR(20) NOT NULL,
STATE CHAR(2) NOT NULL,
ZIP NUMBER(4) NOT NULL,
DATE_HIRED DATE)
STORAGE(INITIAL 3K,
NEXT 1K)
[/code]

You have to select the database where you want to insert the table, there are to ways to do it :
Selecting the default database : USE databasename;
Specifying database name before the table in the CREATE TABLE statement : CREATE TABLE DATABASENAME.EMPLOYEE_TABLE ...
Where databasename is the name of your database.

Related

error when forward engineering MySQL model

I have created a model in MySQL workbench, when I want to forward engineer it to create the "create" and "insert" script I get the following error:
ERROR:
Executing SQL script in server
ERROR: Error 1366: Incorrect integer value: 'G1' for column 'gebruiker_id' at row 1
SQL Code:
INSERT INTO `databaseher`.`gebruiker` (`gebruiker_id`, `voornaam`, `achternaam`, `E-mail`) VALUES ('G1', 'Ronny', 'Giezen', 'r.giezen#gmail.com')
I don't understand whats wrong with it, because the datatype of the column where the value "G1" inserts into is "VARCHAR(4)". It should be possible to insert both a letter and a number.... At least that's what I thought...
This is the create table:
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
If someone could help, that'll be awesome.
Thank you in advance!
Just a guess here because we don't have the full picture.
Can you run this:
Drop table 'databaseher'.'gebruiker'
And after recreate the table
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
rerun the insert.
I am guessing that the table was initially created with the column gebruiker as integer

Error in importing sql file [#1064]

I am trying to create a database for my login/signup forms. I creted this database using SQL. However, when importing it in phpmyadmin it says "Import has been successfully finished, 5 queries executed."
then the error:
Error
SQL query:
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null default "",
password varchar(50) not null default "",
age integer(50) not null,
primary key ('email', 'password')
)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''email', 'password')
)' at line 7
this is my sql code:
drop database if exists loginInfo;
create database if not exists loginInfo;
use loginInfo;
drop table if exists LoginTable;
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null,
password varchar(50) not null,
age integer(50) not null,
primary key ('email', 'password')
);
Remove Single quotes in email, password.
While defining primary key you don't need to add quotes.
drop database if exists loginInfo;
create database if not exists loginInfo;
use loginInfo;
drop table if exists LoginTable;
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null,
password varchar(50) not null,
age integer(50) not null,
primary key (email, password)
);

SQL command that creates a database

Write an SQL command that creates a database that has the following structure:
Table 2: Name: StaffDetails
Screenshot of Structure: Table Structure
CREATE DATABASE StaffDetails_database;
USE StaffDetails _database;
CREATE TABLE StaffDetails (
StaffRef INT NOT NULL,
LName NCHAR(25) NOT NULL,
FName NCHAR(25) NOT NULL,
Phone NCHAR(25) NULL,
StartDate DATE NOT NULL,
PRIMARY KEY (StaffRef),
);
Would this be correct way to create this?

Syntax error, creating database

I am trying to create database android_api and table users but I am getting error
#1064 - Something is wrong in your syntax near 'use android_api
create table 'users'(
id int(11) NOT NULL primary KEY AU' w linii 3
Here is the code
create database android_api
use android_api
create table users(
id int(11) NOT NULL primary KEY AUTO_INCREMENT,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
You should separate each SQL statement with ;, otherwise your syntax is incorrect.
Here is the correct syntax:
create database android_api;
use android_api;
create table users(
id int(11) NOT NULL primary KEY AUTO_INCREMENT,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
In your code what you did was actually:
create database android_api use android_api
(Which is not a valid CREATE statement).
Actually, the accepted answer is enough. This is a very small idea to share after running into the same problem as you report it, but in a beginner's tutorial Load PostgreSQL Sample Database in an SQL Shell.
In general, if you have the reported error in an SQL shell, and if you are not sure whether you have typed ; at the end of the last line, just type ; as a new command and press Enter to be sure that you type your next command in LINE 1 and not in LINE 2.
Without this "trick", some stupidity like this can happen:
postgres=# CREATE DATABASE dvdrental
postgres-# show databases
postgres-# create database dvdrental;
ERROR: syntax error at or near »show«
LINE 2: show databases
^
postgres=# create database dvdrental
postgres-# CREATE DATABASE dvdrental;
ERROR: syntax error at or near »CREATE«
LINE 2: CREATE DATABASE dvdrental;
^
postgres=# CREATE DATABASE dvdrental;
CREATE DATABASE
Finally working, as can be seen by the output CREATE DATABASE.

SQL Error #1007

I have this #1007 error with my SQL code. When I try to import into my database it gives me this #1007 error. The data base is called company. I'm new to SQL and it would be good if someone could help me out. Thanks
CREATE DATABASE company;
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
It looks like the database "company" may already exist. If you look at the link provided by #Marc B, the error "#1007" corresponds to:
"Error: 1007 SQLSTATE: HY000 (ER_DB_CREATE_EXISTS)
Message: Can't create database '%s'; database exists
An attempt to create a database failed because the database already exists.
Drop the database first if you really want to replace an existing database, or add an IF NOT EXISTS clause to the CREATE DATABASE statement if to retain an existing database without having the statement produce an error."
Check your schema to make sure that you don't already have a database called "company" created.
Perhaps you are only trying to create the table "login", whereas you would simply need the code:
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
making sure you create this table in the already existing "company" database.
You are trying to create a database that has already been created.
Staring at your two commands, you could do CREATE TABLE IF NOT EXISTS
you also need to set default database before creating the table
CREATE DATABASE IF NOT EXISTS company;
USE company
CREATE TABLE login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);
or you could put the DB name before the table name
CREATE DATABASE IF NOT EXISTS company;
CREATE TABLE company.login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);