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'.
Related
I have a Student table in SQL Server database which is as follows:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I want the Id property to be alpha-numeric and auto-increment itself for a new entry. I want Id to be S<number> and then S<number+1> and so on.
I tried to solve this problem as a two-step process:
(i) I first tried to make the Id an auto-incrementing property by doing this:
Then I pressed "Update":
And then I updated again and it led me to this table:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I do not think Id is an auto-incrementing value yet. How can I make it both auto-incrementing and alpha-numeric from the following interface:
It seems that you don't really want a fully auto-incrementing alphanumeric column A001,A002...B001, you just want a regular integer column with a prefix of S. For this you can use a simple computed column
ALTER TABLE Student
ADD MyId AS CONCAT('S', Id);
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.
Software: SSMS 2008 R2, Visual Studio 2010 Ultimate (for creating objects)
This is very strange and I would love for someone to be able to explain it to me. I have two versions of the same table. The production version has two fields, and the Test version has the same two fields PLUS a RecordID field (identity field).
Here's the table definition in Test:
CREATE TABLE [dbo].[Table]
(
[Field1] varchar(50) NOT NULL,
[Field2] varchar(50) NOT NULL,
[RecordId] smallint identity not null,
constraint [CIDX_Table] unique clustered ([RecordId]) with (data_compression=page),
)
Here's where the error gets thrown... I try to refresh the Test version with the production data (Field1 & field2). Since the production version doesn't have the identity column, I don't map it. The identity column should auto-increment. My research shows that if you don't explicitly state seed and increment, the default is (1,1). The import task wizard fails (even with Enable Identity Insert checked!). The error:
"Cannot insert NULL value into field RecordID."
Here is the REALLY strange part. Initially, I thought the error was that the seed and increment were missing, so I modified the table definition as follows:
CREATE TABLE [dbo].[Table]
(
[Field1] varchar(50) NOT NULL,
[Field2] varchar(50) NOT NULL,
[RecordId] smallint identity(1,1) not null,
constraint [CIDX_Table] unique clustered ([RecordId]) with (data_compression=page),
)
Then I tried to refresh the table again, and the EXACT SAME SSIS package succeeded and copied both columns and auto-incremented the RecordID field.
My question: What the heck!? Why does explicitly stating seed/increment allow the import wizard to insert values and auto-increment if the default when you do not explicitly state them is still 1,1?
Just remove the NOT NULL in smallint identity of RecordId column. Because the identity is already not null:
CREATE TABLE [dbo].[Table]
(
[Field1] varchar(50) NOT NULL,
[Field2] varchar(50) NOT NULL,
[RecordId] smallint identity(1,1),
constraint [CIDX_Table] unique clustered ([RecordId]) with (data_compression=page),
)
I'm having a problem building some constraints in my MSSQL database. This database will be used for a small electronics store. For the moment I have this to create my database:
CREATE TABLE [dbo].[PRODUCT] (
[ID] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
[ID_SUBCAT] INT NOT NULL,
[KORTING] FLOAT NULL,
[PRIJS] FLOAT NULL,
[TYPE] VARCHAR(5) NOT NULL )
And now I want to add a constraint that the [TYPE] column can only hold certain values. So I came up with this constraint
GO ALTER TABLE [dbo].[PRODUCT] ADD CONSTRAINT [ProperTypeEntered] CHECK ([TYPE] IN ('Camera', 'Lens'))
But when I have a new type that I want to add I always have to edit this constraint because the values are hardcoded in the constraint. So I was hoping that I could make a second table with all the types in like so:
CREATE TABLE [dbo].[TYPES] (
[ID] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
[NAAM] VARCHAR(5) NOT NULL )
And just have a constraint that will allow only that are inserted in the TYPES.NAAM column.
Is that in anyway possible or am I just overlooking something? My knowledge of SQL is limited so any help would be great! :)
Your not able to make a constraint that does a query. But you could do it based on a trigger for insert/update, that would query your second table, if the "type" isn't found you could throw an expection which would cause the insert/update to fail.
The best solution is to create another table, like you're suggesting, only call it ProductTypes. Add a new INT Column called TypeID to dbo.Products. Create a Foreign Key from dbo.Products.TypeID to dbo.ProductTypes.ID. Make sure you have all of the types added into your dbo.ProductTypes table. Also, add a column "IsActive" BIT so that you don't have to delete product types later. If you have any products that aren't in the types table, after you have the foreign key, insert NULL.
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.