Create table does not work - sql

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

Related

Why does it keep giving me te same errors?

So I wanted to put a sql file in a database (es_extended.sql) and it keeps giving me errors like: duplicate column name name etc. and
SQL error (1050): Table 'items' already exists
I have tried to change the names of columns and all but it keeps giving the same errors.
here is the code:
ALTER TABLE `users`
ADD COLUMN `money` VARCHAR(50) DEFAULT NULL,
ADD COLUMN `name` VARCHAR(50) NULL DEFAULT '' AFTER `money`,
ADD COLUMN `skin` LONGTEXT NULL AFTER `name`,
ADD COLUMN `job` VARCHAR(50) NULL DEFAULT 'unemployed' AFTER `skin`,
ADD COLUMN `job_grade` INT NULL DEFAULT 0 AFTER `job`,
ADD COLUMN `loadout` LONGTEXT NULL AFTER `job_grade`,
ADD COLUMN `position` VARCHAR(36) NULL AFTER `loadout`
;
CREATE TABLE `items` (
`name` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
`limit` int(11) NOT NULL DEFAULT '-1',
`rare` int(11) NOT NULL DEFAULT '0',
`can_remove` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`name`)
);
CREATE TABLE `job_grades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_name` varchar(50) DEFAULT NULL,
`grade` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
`salary` int(11) NOT NULL,
`skin_male` longtext NOT NULL,
`skin_female` longtext NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `job_grades` VALUES (1,'unemployed',0,'unemployed','Unemployed',200,'{}','{}');
CREATE TABLE `jobs` (
`name` varchar(50) NOT NULL,
`label` varchar(50) DEFAULT NULL,
PRIMARY KEY (`name`)
;
INSERT INTO `jobs` VALUES ('unemployed','Unemployed');
CREATE TABLE `user_accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identifier` varchar(22) NOT NULL,
`name` varchar(50) NOT NULL,
`money` double NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
CREATE TABLE `user_inventory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identifier` varchar(22) NOT NULL,
`item` varchar(50) NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
Sql databases are a way to implement the persistence layer: data that should be kept permanently or at least independently from sessions.
The error you're getting: SQL error (1050): Table 'items' already exists means that you've already created a table with the same name you're using to try and create a new table.
You do not need to recreate the table each time you try to add data to it. The table is persistent as long as you do not DROP (delete) it or the database it belongs to.
If you want to recreate the table (either because you need to change its schema or because you want to start over), you can run the command DROP TABLE items which will delete the table and all data in the table.
After dropping, you can run the CREATE TABLE items... command again.
If you don't want to delete the table, you can just run the INSERT INTO items... command to add data to the existing table.
The SQL Error (xxxx): duplicate column name 'name' error means you are trying to add a column that already exists in the table. If the users table already has a column called name, then you cannot run the command ALTER TABLE users ADD COLUMN 'name'... without an error.
If the column doesn't meet your needs anymore, you can use ALTER TABLE users ALTER COLUMN 'name'... to change the schema for the column.

A Symbol Name was Expect by 'id"

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

How to debug the error in this SQL command?

I am trying to execute this SQL command to create a table.
CRETE TABLE NEWS (
ID INT(128) NOT NULL AUTO_INCREMENT,
TITLE VARCHAR(128),NOT NULL,
SLUG VARCHAR(128) NOT NULL,
TEXTS_S TEXT NOT NULL,
PRIMARY (ID), );
)
You need:
Create table news (
ID INT(128) NOT NULL AUTO_INCREMENT,
TITLE VARCHAR(128) NOT NULL,
SLUG VARCHAR(128) NOT NULL,
TEXTS_S TEXT NOT NULL,
PRIMARY KEY (ID) );
Missing KEY at the end, bogus comma on the line creating TITLE, another bogus comma on the PRIMARY line. First problem was the full stop in the table name.
Create table news
(
ID INT identity (1,1) primary key ,
TITLE VARCHAR(128) NOT NULL,
SLUG VARCHAR(128) NOT NULL,
TEXTS_S TEXT NOT NULL
)

Execute query in SQL Server Management Studio

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.

Creating a users table in SQL

I'm trying to create a Users table:
CREATE TABLE users
( user_id int(5) PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
But I keep getting this error:
Error starting at line 1 in command:
CREATE TABLE users ( user_id int(5) PRIMARY KEY, username varchar(25) NOT NULL, password varchar(30) NOT NULL )
Error at Command Line:2 Column:13
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Any ideas?
don't try to put precision for integer type:
CREATE TABLE users
( user_id int PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
Looks like it's related to your int(5) data type specification. See Oracle numerica data types.
Try something like:
CREATE TABLE users
( user_id NUMBER PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
try
CREATE TABLE users
( user_id int PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
The following works, but note that the PRIMARY KEY constraint is added at the end of the command
CREATE TABLE users(
user_id int NOT NULL,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL,
PRIMARY KEY(user_id)
);
To create a table:
CREATE TABLE users
( user_id int(5) NOT NULL,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL,
PRIMARY KEY(user_id)
);