Incorrect Syntax on a Table in SQL - sql

I'm building a database for a site and I keep getting an incorrect syntax error for this portion of it:
CREATE TABLE posts (
pid SERIAL PRIMARY KEY,
title VARCHAR(255),
body VARCHAR,
user_id INT REFERENCES users(uid),
author VARCHAR REFERENCES users(username),
date_created TIMESTAMP
like_user_id INT[] DEFAULT ARRAY[]::INT,
likes INT DEFAULT
);
When I put a comma after TIMESTAMP, it then switches the error message to after the INT [] on the like_user_id line.

Assuming you are using Postgres (which the syntax is closest to), you can use something like this:
CREATE TABLE posts (
pid SERIAL PRIMARY KEY,
title VARCHAR(255),
body VARCHAR(255),
author_uid INT REFERENCES users(uid),
date_created TIMESTAMP,
like_user_id INT[] DEFAULT '{}'::INT[],
likes INT DEFAULT 0
);
Notes:
There is no need for a separate foreign key reference for the user and the name. Use a join to get the name.
You need a comma after the timestamp.
You need an appropriate expression for an empty array for like_user_id.
You need a default value for likes.
I also recommend putting in lengths for the varchar(). I mean, you can skip all the lengths if you like (Postgres allows that), but I don't see a reason to have lengths in some columns and no lengths in others.

Cast ARRAY[]::INT => ARRAY[]::INT[]:
CREATE TABLE posts (
pid SERIAL PRIMARY KEY,
title VARCHAR(255),
body VARCHAR,
user_id INT REFERENCES users(uid),
author VARCHAR REFERENCES users(username),
date_created TIMESTAMP,
like_user_id INT[] DEFAULT array[]::int[],
likes INT DEFAULT 0
);
db<>fiddle demo

Related

Incorrect syntax near 'unsigned' Microsoft SQL Server

Im having an error with this query, I'm using a the latest SQL server and management studio
See the query below
CREATE TABLE messages_server (
Id int unsigned NOT NULL AUTO_INCREMENT,
SentTime datetime,
MessageRead BIT,
Content varchar(8000),
MessageCategory varchar(255),
MessageUser varchar(255),
PRIMARY KEY (Id)
)
Microsoft SQL Server does not support unsigned integer types, it also doesn't support the keyword AUTO_INCREMENT. It looks like you have generated a script from another DB engine and tried to run it on SQL Server. The corrected script below should run.
CREATE TABLE messages_server (
Id int NOT NULL IDENTITY,
SentTime datetime,
MessageRead BIT,
Content varchar(8000),
MessageCategory varchar(255),
MessageUser varchar(255),
PRIMARY KEY (Id)
)
As mentioned in the comments, firstly SQL Server doesn't support choice of signing of data types; all numerical data types are signed data types with the exception of tinyint which is an unsigned 1-byte integer. If you want to ensure that a column is always a positive value use a CHECK CONSTRAINT.
T-SQL also doesn't use AUTO_INCREMENT it uses IDENTITY.
As such, you likely want something like this. Note I also name your primary key, as naming your CONSTRAINTs is important:
CREATE TABLE dbo.messages_server (Id int NOT NULL IDENTITY(1, 1),
SentTime datetime,
MessageRead bit, --Should this be NULLable?
Content varchar(8000),
MessageCategory varchar(255),
MessageUser varchar(255),
CONSTRAINT PK_messages_server PRIMARY KEY (Id),
CONSTRAINT chk_id_signed CHECK (Id >= 0));

SQL, How do I add a column which needs to be a primary key, unique and auto-incrementing? Error says syntax error at or near "IDENTITY"

CREATE TABLE cities
(
name varchar(80),
location point
);
ALTER TABLE cities
ADD city_id int IDENTITY(1,1) UNIQUE NOT NULL PRIMARY KEY;
It looks like you have followed documentation for a different RDBMS.
The correct Postgres syntax would be
alter table cities
add city_id int unique primary key not null generated by default as identity;

POSTGRESQL. Insert or update on table violates foreign key constraint

I am new in Posgresql. I have 5 tables and I am trying to INSERT properties to tables. When I tried to Insert 2nd time, I have this error in 'pgadmin'.
ERROR: insert or update on table "question" violates foreign key constraint "question_id_difficulty_fkey" DETAIL: Key (id_difficulty)=(9) is not present in table "difficulty". SQL state: 23503.
my schema is here
id SERIAL PRIMARY KEY,
name varchar
);
CREATE TABLE question (
id SERIAL PRIMARY KEY,
text varchar,
correct_answer varchar,
incorrect_answer1 varchar,
incorrect_answer2 varchar,
incorrect_answer3 varchar,
id_difficulty SERIAL REFERENCES difficulty(id),
id_category SERIAL REFERENCES category (id),
id_creator SERIAL REFERENCES game (id)
);
CREATE TABLE difficulty (
id SERIAL PRIMARY KEY,
name varchar
);
CREATE TABLE category (
id SERIAL PRIAMRY KEY,
name varchar
);
CREATE TABLE user (
id SERIAL PRIMARY KEY,
name varchar
)
You would need a corresponding entry in the difficulty table with an id of 9, so that a referencing id_difficulty column in the question table.
For example, if your difficulty table contained:
id | name
----+----------------
1 | easy
2 | reasonable
3 | difficult
4 | very difficult
5 | impossible
You could only set id_difficulty for rows in the question table to one of those id values. If you set 6, or 12 or anything other than 1 to 5, it would fail because the values are constrained by the values in the foreign key.
The id_difficulty, id_category and id_creator columns shouldn't be using serial, so these should have their defaults dropped:
ALTER TABLE question ALTER COLUMN id_difficulty DROP DEFAULT;
ALTER TABLE question ALTER COLUMN id_category DROP DEFAULT;
ALTER TABLE question ALTER COLUMN id_creator DROP DEFAULT;
Postgres now recommends using generated always as instead of serial. If you do this, then the types will align much more simply:
CREATE TABLE question (
id int generated always as identity PRIMARY KEY,
text varchar,
correct_answer varchar,
incorrect_answer1 varchar,
incorrect_answer2 varchar,
incorrect_answer3 varchar,
id_difficulty int REFERENCES difficulty(id),
id_category int REFERENCES category (id),
id_creator int REFERENCES game (id)
);
CREATE TABLE difficulty (
id int generated always as identity PRIMARY KEY,
name varchar
);
This makes what is happening much clearer. The data type for a foreign key reference needs to match the data type of the primary key. Postgres knows that serial is really int. But using generated always, it is obvious that they are the same.
In addition, generated always as is more consistent with standard SQL.

Creating a table and getting error as ora 00907 missing right parenthesis

I am trying to create a table and getting error as ora 00907 missing right parenthesis. What went wrong?
This is my query:
CREATE TABLE College (
ID int,
NAME varchar(255),
Branch var char(255) NOT NULL,
Percentage int,
Address varchar,
City varchar(255),
PRIMARY KEY (ID)
);
You have multiple errors. Presumably, you want something like this:
CREATE TABLE Colleges (
CollegeID int PRIMARY KEY,
Name varchar2(255),
Branch varchar2(255) NOT NULL,
Percentage int,
Address varchar2(255),
City varchar2(255)
);
Notes:
Your syntax problem is the space in var char.
Oracle recommends varchar2() over varchar.
You should always include a length in the definition.
You can inline the primary key definition.
I prefer that the primary key include the entity name, rather than the generic id.
Similarly, I prefer that the table be in the plural, because it contains multiple colleges.
A column called percentage with a type of int is suspicious.

Can a SERIAL foreign key be set to null?

Question: I have a table with a couple of foreign keys. I want to make two of the foreign keys optional (that is, they can be set to NULL). When I try to run the command to create the table, I get the error:
ERROR: conflicting NULL/NOT NULL declarations for column "activityid" of table "eventassociation"
If I try and create the table without the NULL declaration for the foreign keys, those foreign keys implicitly become NOT NULL.
How can I make it so that the foreign keys activityID and roleID can be null?
Thanks,
hypoz
Extra information:
I am running postgreSQL (version on apt-get) on ubuntu 12.04.
This is the problematic table:
CREATE TABLE eventAssociation (
associationID SERIAL PRIMARY KEY,
studentID VARCHAR(12) references volunteers(studentID),
eventID SERIAL references events(eventID),
activityID SERIAL references activities NULL,
roleID SERIAL references roles(roleID) NULL,
coordinatorConfirmed BOOLEAN,
userRevokeable BOOLEAN
)
And for reference, here is the schema for the activities table. The roles table is pretty much identical except with different field names.
CREATE TABLE activities (
activityID SERIAL PRIMARY KEY,
eventID integer references events(eventID),
name varchar(128),
description TEXT
)
Defining a column that is a FK as serial does not make any sense. It means that whenever you don't supply a value, a new id will be generated - I cannot imagine any situation where that makes sens.
I think you actually want to define those columns as integer:
CREATE TABLE activities
(
activityID SERIAL PRIMARY KEY,
eventID INTEGER REFERENCES events,
name VARCHAR(128),
description TEXT
);
CREATE TABLE eventAssociation
(
associationID SERIAL PRIMARY KEY,
eventID INTEGER REFERENCES events,
activityID INTEGER REFERENCES activities,
coordinatorConfirmed BOOLEAN,
userRevokeable BOOLEAN
);