SQL Server 2008 R2. Incorrect syntax near 'AUTO_INCREMENT' - sql

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.

Related

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;

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.

Cannot define PRIMARY KEY constraint on nullable column in table 'tp_ladder'

I'm trying to run the following SQL statement
--dropping customer TABLE--
DROP TABLE tp_ladder;
--creating ladder TABLE
CREATE TABLE tp_ladder (
ladder_id INTEGER,
ladder_type VARCHAR(50),
ladder_name VARCHAR(50) NOT NULL,
ladder_discount DECIMAL(3,2),
ladder_price DECIMAL(8,2) NOT NULL,
ladder_weight DECIMAL(5,2),
ladder_height DECIMAL(5,2),
ladder_rating DECIMAL(10,2),
warehouse_id INTEGER
);
--creating primary key for ladder table
ALTER TABLE tp_ladder
ADD CONSTRAINT tp_ladder_ladder_id
PRIMARY KEY(ladder_id);
However I receive the error message:
Cannot define PRIMARY KEY constraint on nullable column in table 'tp_ladder'
Any advice?
The error is quite clear, but why it is an error is not obvious.
Other databases (such as MySQL and Postgres), do allow you to do what you want -- adding a primary key on a column that is not explicitly declared as NOT NULL. After all, PRIMARY KEY imposes a NOT NULL constraint as well. This is surprising, especially on an empty table. So, the error is not obvious.
Further, SQL Server stores the null flags for all columns, even those that are declared NOT NULL. So, even if there were data, then the data would not need to change (assuming there are no NULL values). Not all databases store NULL flags the same way.
If you have defined the table, you can modify the column using:
ALTER TABLE tp_ladder ALTER COLUMN ladder_id INT NOT NULL
This will allow you then add the primary key constraint.
But, I recommend doing it in-line when you create the table:
ladder_id INT PRIMARY KEY
Note that when defined in the CREATE TABLE, the NOT NULL is not needed (it is actually redundant).

Foreign key on same table

I have a db schema that includes
Group{name, group.id, parent.id} with key {group.id}
In this schema all parent.id's must either already exist in the group.id column or be null.
How can i translate this constraint into SQL while creating the table?
Thanks
A regular foreign key should suffice. It won't perform any checks if the field is null. The precise syntax may depend slightly on the SQL dialect, but it would look something like
create table Group_ (
name varchar(30) not null,
groupid int not null primary key,
parentid int null foreign key references Group_ (groupid) )

IDENTITY NOT NULL at Table Creation

Can anyone please tell me whether the instruction IDENTITY NOT NULL at a table creation is redundant or not? I mean, judging by the message
DEFAULT or NULL are not allowed as explicit identity values.
I would say that any column declared as IDENTITY is implicitly also declared as NOT NULL, but I would like to make sure. Can anyone please confirm?
Thank you very much.
SQL Server adds NOT NULL constraint to identity columns automatically eventhough he did not speficy it when creating a table
Consider the following table script
create table test(id int identity(1,1), name varchar(1000))
Now Generate the script of the table from Management Studio. It generates the script as
CREATE TABLE [dbo].[test](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](1000) NULL
) ON [PRIMARY]
Eventhough NOT NULL constraint is not specified in the table script by default it is added. The identity column will never be NULL. So NOT NULL constraint is added default
SQL Server (2008, and probably earlier versions as well) will not allow you to create an identity column on a NULL column. Try it:
CREATE TABLE Foo1
(
FooId int identity not null
,Data varchar(20) not null
)
works, where
CREATE TABLE Foo2
(
FooId int identity null
,Data varchar(20) not null
)
generates error message Could not create IDENTITY attribute on nullable column 'FooId', table 'Foo2'.