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.
Related
I am using Sequel Pro with MySQL on my Mac. I return the error: "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 ')' " every time I attempt to run this sql query. The Database is created successfully, but the table is not. Can anyone advise where the error in my syntax is? I simply do not see it:
CREATE DATABASE bamazon
USE bamazon;
CREATE TABLE products
(
`id` int NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
`department_name` varchar(45) NOT NULL,
`price` int NOT NULL,
`stock_quantity` int NOT NULL,
PRIMARY KEY (id),
);
Please remove the ',' after PRIMARY KEY (id). It should be like this
CREATE TABLE products
(
id int NOT NULL AUTO_INCREMENT,
product_name varchar(45) NOT NULL,
department_name varchar(45) NOT NULL,
price int NOT NULL,
stock_quantity int NOT NULL,
PRIMARY KEY (id)
);
The query does not work because there is an extra comma (,) at the end. Remove that and it will work. The correct syntax will be:
CREATE TABLE products
(
id int NOT NULL AUTO_INCREMENT,
product_name varchar(45) NOT NULL,
department_name varchar(45) NOT NULL,
price int NOT NULL,
stock_quantity int NOT NULL,
PRIMARY KEY (id)
);
With this code:
create table jogadores(
id INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT,
nome varchar NOT NULL,
idade int NOT NULL UNSIGNED,
nacionalidade varchar NOT NULL
)
I keep getting this error:
)
Error report -
SQL Error: 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,
idade int NOT NULL UNSIGNED,
nacionalidade varchar NOT NULL
)' at line 3.
Also, I get the red underline under the "T" and the "(" in "INT(10)".
varchar() should have a length:
create table jogadores (
id INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT,
nome varchar(255) NOT NULL,
idade int UNSIGNED NOT NULL,
nacionalidade varchar(255) NOT NULL
);
And UNSIGNED needs to go immediately after the numeric declaration, not after NOT NULL. See here.
You need to specify the length of the varchar i.e. varchar(100).
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.
Perhaps I'm just too used to Postgres but why am I getting this error
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 '{ id int not null AUTO_INCREMENT, email varchar(100) not null,
primary key(id' at line 1
when I run this?
create table `users`{
id int not null AUTO_INCREMENT,
email varchar(100) not null,
primary key(id)
};
Use parenthesis (normal brackets) () not braces:
create table `users` (
id int not null AUTO_INCREMENT,
email varchar(100) not null,
primary key(id)
);
the correct syntax is :
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(100) NOT NULL,
PRIMARY KEY(id)
);
check it out here for your version:
https://dev.mysql.com/doc/refman/4.1/en/creating-tables.html
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;