Incorrect syntax near 'unsigned' Microsoft SQL Server - sql

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

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.

SQL syntax error while creating table in MariaDB

While creating a table in Mariadb with this code
CREATE TABLE classes(
ClassID SMALLINT UNSIGNED PRIMARY,
Grade TINYINT UNSIGNED,
Subject VARCHAR(20),
YearTaught YEAR
);
I got this error.
#1064 - 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 '
Grade TINYINT UNSIGNED,
Subject VARCHAR(20),
YearTaught YEAR
)' at line 2
And I don't know what's wrong with the syntax. Thank you.
You are missing the keyword KEY on your statement, update
ClassID SMALLINT UNSIGNED PRIMARY,
to
ClassID SMALLINT UNSIGNED PRIMARY KEY,
You could use KEY instead of PRIMARY KEY, but not just PRIMARY:
Use PRIMARY KEY (or just KEY) to make a column a primary key. A primary key is a special type of a unique key. There can be at most one primary key per table, and it is implicitly NOT NULL.
The documentation is here.

I keep getting parenthesis error in SQL Oracle when introducing a foreign key

I have been searching and playing around with the code trying to figure out why its giving me parenthesis error when I followed the format of Oracle referencing in other tables
Create table TourOperator (TOID int PRIMARY KEY, Cname varchar (20), phone long int);
Create table Airline(Aname varchar(20) PRIMARY KEY, Website varchar(255), Phone int(10), TOID int, FOREIGN KEY(TOID) REFERENCES TourOperator);
You have a few issues in your SQL, according to the Oracle syntax:
The type INT cannot be qualified with a size.
The type VARCHAR technically does exist in Oracle, but is not recommended; use VARCHAR2 instead.
The syntax of the foreign key clause is wrong.
Avoid the type LONG; it's a legacy type that you shouldn't use for new databases. I changed the phone column to VARCHAR2.
The SQL can run as:
Create table TourOperator (
TOID int PRIMARY KEY,
Cname varchar2(20),
phone varchar2(20)
);
Create table Airline(
Aname varchar2(20) PRIMARY KEY,
Website varchar2(255),
Phone int,
TOID int REFERENCES TourOperator (TOID)
);
See running example at db<>fiddle.
You need to specify the target column of the foreign key in the TourOperator table. Try this:
Create table TourOperator
(TOID int PRIMARY KEY,
Cname varchar2(20),
phone long int
);
Create table Airline
(Aname varchar2(20) PRIMARY KEY,
Website varchar2(255),
Phone int,
TOID int,
FOREIGN KEY(TOID) REFERENCES TourOperator(TOID)
);
For more examples, see here: https://www.oracletutorial.com/oracle-basics/oracle-foreign-key/

Incorrect Syntax on a Table in 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

SQL Server 2008 R2. Incorrect syntax near 'AUTO_INCREMENT'

Why do i get the following error
Incorrect syntax near 'AUTO_INCREMENT'.
while trying to execute
CREATE TABLE Person
(
P_Id int NOT NULL AUTO_INCREMENT,
Name varchar(255),
PRIMARY KEY (P_Id)
)
What is the correct syntax?
CREATE TABLE Person(
P_Id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name varchar(255))
You should explicitly state whether NAME is NULL or NOT NULL so you are not dependant upon the current connection settings that happen to be in effect.
create table Person
(
PersonId int identity(1,1)
constraint PK_Person primary key,
Name varchar(255) not null
)
Some comments:
It is not needed to specify not null for identity column as identity column cannot be nullable. ANSI_NULL_DFLT_ON option does not affect 'nullability' of identity column.
On other hand it is important to specify 'not null / null' for Name column, as it will be affected by ANSI_NULL_DFLT_ON value.
It is always a good idea to explicitly specify names for constraints. Because if you don't, name constraint name will be generated. If you need to delete the constraint later, you will have to find out the auto-generated name.