I'm trying to make a table for a login system for a simple website project but I'm having this error that I wasn't having the other day. I'm getting the errors at 'id' and unsigned on the 2nd line. The error is "A Symbol Name is Expected Near 'id'. I also get an error by unsigned on the same line that says "unrecognizable statement type". I am using XAMPP.
CREATE TABLE 'users' (
'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
'username' varchar(75) NOT NULL,
'password' varchar(255) NOT NULL,
'email' varchar(100) NOT NULL,
'phone' varchar(15) NOT NULL,
'address' varchar(255) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE KEY 'email' ('email')
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
You're using single quotes for identifiers (column and table names). Single quotes are for strings.
Identifiers are either unquoted, or use the MySQL/MariaDB specific `. The SQL standard uses ". Consider turning on ANSI mode for compatibility.
Also consider using the serial shorthand for bigint unsigned not null auto_increment unique.
create table users (
id serial primary key,
...
)
Related
I am trying to create a table with this query:
CREATE TABLE "real_estate_static" (
"uid" SERIAL NOT NULL DEFAULT NEXTVAL("real_estate_uid_seq") PRIMARY KEY,
"source_id" JSON NOT NULL,
"source_url" TEXT NOT NULL,
"created_at" TIMESTAMP NULL DEFAULT NULL,
"address" TEXT NOT NULL,
"city" TEXT NOT NULL,
"state" TEXT NOT NULL,
"zip" INTEGER NULL DEFAULT NULL,
"latitude" NUMERIC NULL DEFAULT NULL,
"longitude" NUMERIC NULL DEFAULT NULL,
"type" TEXT NOT NULL,
"category" TEXT NOT NULL,
"square_ft" NUMERIC NULL DEFAULT NULL,
"acres" NUMERIC NULL DEFAULT NULL,
"images" JSON NULL DEFAULT NULL
);
Error:
multiple default values specified for column "uid" of table "real_estate_static"
I thought that maybe a mistake due to the parameter NOT NULL, but removing it did not change anything. Most of the problems that I found on the Internet with such an error are due to Django, or other ORMs, but this is not my case, so I could not get around this.
serial is not a real type, but an alias which includes a default.
From the docs....
The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases). In the current implementation, specifying:
CREATE TABLE tablename (
colname SERIAL
);
is equivalent to specifying:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
So all that nextval and not null is unnecessary. serial does it for you.
"uid" SERIAL PRIMARY KEY,
I have a problem with creating a table in Maria DB Server.
I could successfully make the table on my other device, but for MYSQL (with probably different code).
Query:
CREATE TABLE admin (
'id' double(9999) NOT NULL auto_increment,
'username' VARCHAR(50) NOT NULL,
'passcode' VARCHAR(50) NOT NULL,
'email' VARCHAR(50) NOT NULL,
'state' VARCHAR(50) NOT NULL,
'points' double(9999) not null,
PRIMARY KEY(id)
);
Query 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 "id' double(9999) NOT NULL auto_increment 'username' VARCHAR(50) NOT NULL, 'pa' at line 2
If any other information is required ask in comments.
Thanks
You were putting single quotes around the table names, which MariaDB was interpreting as string literals. Instead, just use the unquoted names directly:
CREATE TABLE admin (
id double(9999) NOT NULL auto_increment,
username VARCHAR(50) NOT NULL,
passcode VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
state VARCHAR(50) NOT NULL,
points double(9999) not null,
PRIMARY KEY(id)
);
If you do need to escape a column name in MariaDB/MySQL, e.g. because the name has whitespace or is a keyword, you can do so using backticks:
CREATE TABLE admin (
`id` double(9999) NOT NULL auto_increment,
`username` VARCHAR(50) NOT NULL,
...
)
But note that putting whitespace into column or table names, or using reserved keywords, is considered bad practice.
You could also put the PRIMARY KEY right after AUTO_INCREMENT
CREATE TABLE admin (
'id' double(9999) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'username' VARCHAR(50) NOT NULL,
'passcode' VARCHAR(50) NOT NULL,
'email' VARCHAR(50) NOT NULL,
'state' VARCHAR(50) NOT NULL,
'points' double(9999) not null
);
This very simple query I am trying to execute in SQL Server Management Studio. But it gives error:
CREATE TABLE `contact` (
`contact_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`telephone` varchar(45) NOT NULL,
PRIMARY KEY (`contact_id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8
Error is:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I also tried by replacing with'` but still no help
The backtick (`) character is only used for MySql. It's not standard. Sql Server is a completely different animal. You need to use square-brackets ([ and ] -- also not standard) to enclose table and column names with Sql Server. Sql Server also supports double quotes ("), which are part of the SQL Standard, but for some reason less-common.
While I'm at it, the ENGINE and AUTO_INCREMENT, and CHARSET clauses are also MySql-specific.
Try this:
CREATE TABLE contact (
contact_id int IDENTITY(25,1) NOT NULL PRIMARY KEY,
[name] varchar(45) NOT NULL,
email varchar(45) NOT NULL,
"address" varchar(45) NOT NULL,
telephone varchar(45) NOT NULL
)
in tsql
CREATE TABLE contact (
contact_id int identity(0,25) NOT NULL,
name nvarchar(45) NOT NULL,
email nvarchar(45) NOT NULL,
address nvarchar(45) NOT NULL,
telephone nvarchar(45) NOT NULL
CONSTRAINT contact_PK PRIMARY KEY (contact_id)
)
You can't specify engine.
identity(0,25) means initial value = 0, increment = 25
You don't specify character set for the table, you can declare individual columns as varchar or nvcarchar -- the n stands for unicode. You can also specify a collation sequence for each column.
If you want a column name that is irregular (keyword, embedded space, etc.) you quote the column name as [column name] -- don't use irregular column names if you have a choice, it just makes things a pain to use.
Try like this:
CREATE TABLE contact (
contact_id[int] IDENTITY(1,1) NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
address varchar(45) NOT NULL,
telephone varchar(45) NOT NULL,
PRIMARY KEY (contact_id)
)
Hope this may help you.
CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);
I'm getting this error in puTTY. Not sure why, looks right to me ...
psql:pierre.sql:10: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
^
psql:pierre.sql:18: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
--DROP TABLE customer, reservation;
CREATE TABLE customer(
c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_ref VARCHAR(30) NOT NULL,
f_name VARCHAR(30) NOT NULL,
l_name VARCHAR(30) NOT NULL,
address VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(11) NOT NULL
);
CREATE TABLE reservation(
r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_id VARCHAR(30) NOT NULL REFERENCES customer(c_id),
book_date DATE NOT NULL CHECK (book_date <= now()),
s_time DOUBLE NOT NULL,
e_time DOUBLE NOT NULL,
amount INTEGER NOT NULL
);
Any ideas why?
auto_increment looks like something you'd use with MySQL.
But, here, it seems you are using PostgreSQL.
According to the datatype serial section of the manual, postgresql's equivalent of auto_increment is serial or bigserial.
Quoting that page :
The data types serial and bigserial are not true types, but merely
a notational convenience for setting up unique identifier columns
(similar to the AUTO_INCREMENT property supported by some other databases).
In Postgres 10 or later consider an IDENTITY column:
CREATE TABLE customer(
c_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
...
(PRIMARY KEY also makes it NOT NULL automatically.)
Details:
Auto increment table column
In Postgres 9.6 or older consider a serial like Pascal already suggested.
Works in pg 10 or later, too, but IDENTITY is generally superior.