Sql syntax error on DROP TABLE - mysql

I am trying to create a MySQL table with the following code:
CREATE DATABASE
IF NOT EXISTS myusers;USE
DROP TABLE
DROP TABLE IF EXISTS `myusers`.`users`;CREATE TABLE `myusers`.`users`
(
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NULL,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NULL,
`phone` INT NULL,
PRIMARY KEY (`username`)
)
However, I am getting this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'DROP TABLE
DROP TABLE IF EXISTS myusers.users' at line 2
I have limited knowledge of MySQL. From what I know about SQL syntax this looks fine.
Any idea what could be the issue here?

USE should be followed by a database name.
There is extra DROP Table.
Drop the table if exists DROP TABLE IF EXISTS users first.
Then create the table.
Like this:
CREATE DATABASE IF NOT EXISTS myusers;
USE myusers;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `myusers`.`users`
(
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NULL,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NULL,
`phone` INT NULL,
PRIMARY KEY (`username`)
);

Related

mysql importing error on mysql host

Error
SQL query:
CREATE TABLE `usersinputs` (
`ID` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`message` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
MySQL said: Documentation
#1050 - Table 'usersinputs' already exists
Your error says it all. There is already a table named as userinputs in your database. You need to create a table with a different name or if there is any datatype change or any other alteration to the table then you need to ALTER the table.
Instead of creating a table which already exists try one with another name.
You can however alter or check if the table exists:
CREATE TABLE IF NOT EXISTS `usersinputs` (
`ID` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`message` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
The error is due to the table usersinputs is already exists in your database.
If you really want to CREATE a new table with the different columns and data types you can DROP the existing table and CREATE the new table by DROP TABLE IF EXISTS `usersinputs`;
So your code will be:
DROP TABLE IF EXISTS `usersinputs`;
CREATE TABLE `usersinputs` (
`ID` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`message` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Or simply want to change any column name or data type use ALTER TABLE

I keep getting an error when trying to make a table in phpmyadmin

when I was trying to run this code:
USE user;
CREATE TABLE IF NOT EXISTS 'posts' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'body' text NOT NULL,
'date_added' date NOT NULL,
'added_by' varchar(255) NOT NULL,
'user_posted_to' varchar(255) NOT NULL,
PRIMARY KEY ('id')
)ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
It gave me this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near ''posts' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'body' text NOT NULL,
'dat' at line 1
how do I solve it? By the way, I already selected a database? Any help would be appreciated.
Get rid of all the "'" marks.
CREATE TABLE IF NOT EXISTS posts (
id int(11) NOT NULL AUTO_INCREMENT,
body text NOT NULL,
date_added date NOT NULL,
added_by varchar(255) NOT NULL,
user_posted_to varchar(255) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
Here is a fiddle with a working creation.

error when creating table SQL

I'm trying to create a table in my gym_system database. Here is the code I'm using:
CREATE TABLE `Gym_System`.`login` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL,
`password` CHAR(128) NOT NULL,
) ENGINE = InnoDB;
However, I keep getting this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE = InnoDB' at line 5
Can anyone see why?
Remove the last comma
CREATE TABLE `login` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL,
`password` CHAR(128) NOT NULL
) ENGINE = InnoDB;
See fiddle demo

ERROR 1064 (42000): You have an error in your SQL syntax;

I have a MySQL commands:
CREATE DATABASE IF NOT EXISTS courses;
USE courses
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VAR_CHAR(50) NOT NULL,
addr VAR_CHAR(255) NOT NULL,
phone INT NOT NULL,
);
When I run it, I get an error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'VAR_CHAR(50) NOT NULL, addr VAR_CHAR(255) NOT
NULL, phone INT NOT NULL, )' at line 3
It is varchar and not var_char
CREATE DATABASE IF NOT EXISTS courses;
USE courses;
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
addr VARCHAR(255) NOT NULL,
phone INT NOT NULL
);
You should use a SQL tool to visualize possbile errors like MySQL Workbench.
Try this:
Use back-ticks for NAME
CREATE TABLE `teachers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`addr` varchar(255) NOT NULL,
`phone` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Use varchar instead of VAR_CHAR and omit the comma in the last line i.e.phone INT NOT NULL
);. The last line during creating table is kept "comma free".
Ex:- CREATE TABLE COMPUTER
(
Model varchar(50)
);
Here, since we have only one column ,that's why there is no comma used during entire code.

MySql syntax error while trying to create a table

I am trying to create a table in my database (hosted with godaddy)
when ever I try to create a table in the database it gives me an error
#1046 - No database selected
So I am trying to create a table like this
USE orderformuser
CREATE TABLE `users`(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(25),
`password` char(40),
UNIQUE (User_Id)
)
However I am getting this error now.. and I just don't know what I am doing wrong
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `users`(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(2' at line 3
any help would be appreciated
You forgot to add semicolon ; after use .... ; is used to terminate lines in mysql.
USE orderformuser;
CREATE TABLE `users`
(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(25),
`password` char(40),
CONSTRAINT u_pk PRIMARY KEY (User_Id)
);
First of all you have to create a blank database orderformuser.
After you have to entry into this database and try to create the table with your code changed because you have forgot ";" like this:
USE orderformuser;
CREATE TABLE `users`
(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(25),
`password` char(40),
UNIQUE (User_Id)
)