MySql syntax error while trying to create a table - mysql

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)
)

Related

MySql table doesn't create when I try to add "AUTO_INCREMENT"

CREATE TABLE `userinfo`.`users`
( id MEDIUMINT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR NOT NULL ,
`password` VARCHAR NOT NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
Could anyone give me an insight as to why this create table function isn't working? Error code:
#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 'NOT NULL ,
password VARCHAR NOT NULL ,
PRIMARY KEY (id))
ENGINE ...' at line 3
new to mysql so i have no clue what the problem is. I just want a table that has the primary key increment when i add to it. Thanks in advance.
CREATE TABLE `userinfo`.`users`
( id MEDIUMINT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(50) NOT NULL ,
`password` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
You need to specify maximum character limit for VARCHAR type as above. You can set it as per your requirement.

Sql syntax error on DROP TABLE

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`)
);

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.

learning sql creating table gives error

I am learning to use phpmyadmin and there is a database tshirtshop which has a table department now I want to create a category table but the sql in phpmyadmin is giving me error following is SQL query
CREATE TABLE 'category' (
'category_id' INT NOT NULL AUTO_INCREMENT,
'department_id' INT NOT NULL,
'name' VARCHAR(100) NOT NULL,
'description' VARCHAR(1000),
PRIMARY KEY ('category_id'),
KEY 'idx_category_department_id' ('department_id')
) ENGINE=MyISAM;
and following is the 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
''category' ( 'category_id' INT NOT NULL AUTO_INCREMENT, 'department_id' INT NOT ' at line 1
what should I do to eliminate the error?
Correct query is
CREATE TABLE `category` (
`category_id` INT NOT NULL AUTO_INCREMENT,
department_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
description VARCHAR(1000),
PRIMARY KEY (`category_id`),
KEY `idx_category_department_id` (`department_id`)
) ENGINE=MyISAM;
you cannot use single quote to enclose field name or table name, either use tilt ` or don't use anything. in my query I try to use both just to show you.
Try change the escape char from ' to ` . MySQL thinks that youre object names are strings.
CREATE TABLE `category` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`department_id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`description` VARCHAR(1000),
PRIMARY KEY (`category_id`),
KEY `idx_category_department_id` (`department_id`)
) ENGINE=MyISAM;