Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql create multiple tables

Tags:

sql

mysql

I'm working on a project in which i need to create two tables in one query.

I'm writing like this:

DROP TABLE Employee;

CREATE TABLE Employee(
Employee_Id CHAR(12)NOT NULL PRIMARY KEY,
First_name CHAR(30),
Last_name CHAR(30),
Address VARCHAR(50),
City CHAR,
State CHAR,
Salary INT,
Gender CHAR,
Age INT
);

DROP TABLE Job;

CREATE TABLE job(
Exempt_Non_Exempt_Status tinyint(1) NOT NULL PRIMARY KEY,
Job_title CHAR,
Job_description CHAR
); 

But this gives an error like "Unknown table 'job'" even if I didn't create it.

like image 322
Adi Avatar asked Jul 16 '26 12:07

Adi


2 Answers

Use the DROP Table IF EXISTS syntax:

Use IF EXISTS to prevent an error from occurring for tables that do not exist.

Something like:

DROP TABLE IF EXISTS
  Employee ;

CREATE TABLE Employee(
...
);

DROP TABLE IF EXISTS
  Job ;

CREATE TABLE Job(
...
);
like image 74
ypercubeᵀᴹ Avatar answered Jul 19 '26 00:07

ypercubeᵀᴹ


You cant drop a table that doesnt exist. Use the:

 DROP TABLE IF EXISTS Job;
like image 32
Authman Apatira Avatar answered Jul 19 '26 02:07

Authman Apatira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!