Error in my SQLite syntax - sql

New to SQLite so I don't know what I'm doing wrong. I'm just getting an error saying:
SQLSTATE[HY000]: General error: 1 near "CREATE": syntax error
Here's my SQL:
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY,
date_created DATETIME NOT NULL,
date_updated DATETIME NOT NULL,
username VARCHAR(32) NOT NULL,
password VARCHAR(32) NOT NULL,
role VARCHAR(32) NOT NULL DEFAULT 'member',
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(128) NOT NULL
)
CREATE TABLE subscribers (
id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(40) DEFAULT NULL,
email VARCHAR(255) NOT NULL UNIQUE
)
CREATE TABLE weekly_download (
id INTEGER NOT NULL PRIMARY KEY,
filename TEXT NOT NULL,
download_date DATE NOT NULL,
body TEXT
)

put a semicolon after each statement.
CREATE TABLE ( ... ) ;
CREATE TABLE ( ... ) ;

Start with simple statements using the sqlite3 CLI.
Then, if you forget a ;, you will get quick feedback and can build up to more complex SQL.
$ sqlite3 /tmp/test.db
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> create table badsyntax;
SQL error: near ";": syntax error
sqlite> create table abc (x,y);
sqlite>

Don't forget semi-colons!

Related

Incorrect syntax in SQL Server query

CREATE TABLE identity (
empid VARCHAR(255) PRIMARY KEY,
entry VARCHAR(255)
);
-- Table: vectors
CREATE TABLE vectors (
f_id INTEGER PRIMARY KEY IDENTITY(1,1),
label STRING NOT NULL,
empid STRING REFERENCES identity (empid)
NOT NULL,
vector BLOB NOT NULL
);
I tried to run the above query but it gives me error
Incorrect syntax near expected '.', ID or QUOTED_ID.
I don't understand why it is giving me this error, is it because IDENTITY is a keyword in SQL Server. Kindly help!
Try this:
CREATE TABLE [identity] (
empid VARCHAR(255) PRIMARY KEY,
entry VARCHAR(255)
);
-- Table: vectors
CREATE TABLE vectors (
f_id INTEGER PRIMARY KEY IDENTITY(1,1),
label VARCHAR(255) NOT NULL,
empid VARCHAR(255) REFERENCES [identity] (empid)
NOT NULL,
vector VARCHAR(255) NOT NULL
);
Also, if you are working with SQL Server Management Studio, you can see that some words are colored in blue. It will be better to avoid them using in your code as names of tables, variables and other objects. For example, identity is such word.

postgresql table fails to create

create table users (
id VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL,
password VARCHAR(50) NOT NULL,
);
gokitexample=# \i /Users/henryhudson/OneDrive/Henry/Programming/Go/go-kit/account/users.sql
psql:/Users/henryhudson/OneDrive/Henry/Programming/Go/go-kit/account/users.sql:5:
ERROR: syntax error at or near ")"
LINE 5: );
The error is quite explicit. There is an additional comma at the end of the last column. It should be like that:
create table users (
id VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL,
password VARCHAR(50) NOT NULL
);

SQL auto increment pgadmin 4

I am trying to make a simple database with an number generator but why do I get the error below?
ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: IDNumber int NOT NULL AUTO_INCREMENT,
Code:
CREATE TABLE Finance
(
IDNumber int NOT NULL AUTO_INCREMENT,
FinName varchar(50) NOT NULL,
PRIMARY KEY(IDNumber)
);
The subject of the question mentions pgAdmin 4, so here's how to do it there.
First, add a column to your table, then click the little edit icon:
Then go to Constraints and select the Identity type:
This generates SQL similar to this:
CREATE TABLE public.my_table_name
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
PRIMARY KEY (id)
);
For Postgres you have to use SERIAL
CREATE TABLE Finance
(
IDNumber SERIAL PRIMARY KEY,
FinName varchar(50) NOT NULL
);
IN pgadmin-2.
step 01:
create seq:
and set info:
step 02: go to ID in table and set constraints
if it is sql the syntax is the following
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
You are using a MySQL syntax which won't work in SQL Server.
CREATE TABLE Finance
(
IDNumber int NOT NULL IDENTITY(1,1) PRIMARY KEY,
FinName varchar(50) NOT NULL
);
The following code should work for SQL Server.
IDENTITY(1,1) is SQL Server's way of saying "auto increment".
The starting value for IDENTITY is 1, and it will increment by 1 for each new record.
So : IDENTITY(startvalue, incrementvalue)
there are two options; one is to use the "datatype" serial or create a sequence and use this sequence as a default value for your integer as follows:
CREATE SEQUENCE your_seq;
CREATE TABLE foo(
id int default nextval('your_seq'::regclass),
other_column TEXT);
INSERT INTO foo(other_column) VALUES ('bar') RETURNING *;
It is important to note that specifying a table as follows:
CREATE TABLE tablename (
colname SERIAL);
is equivalent to:
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;
More information about serial can be found here.

Create table does not work

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

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