Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create SQL identity as primary key?

create table ImagenesUsuario {     idImagen int primary key not null IDENTITY } 

This doesn't work. How can I do this?

like image 878
Sergio Tapia Avatar asked Aug 31 '09 00:08

Sergio Tapia


People also ask

Can we make identity column as primary key?

It is possible, however, it has to do done in the design mode and you will have to specify the column in question as identity. Right click on the table, pick "Design", go to the column, in the "Column Properties" look for "Identity Specification" and you are all set.

What is primary key identity in SQL?

A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary keys must contain unique values. A primary key column cannot have NULL values. A table can have only one primary key, which may consist of single or multiple fields.

Should I use primary key as identity?

An identity is simply an auto-increasing column. A primary key is the unique column or columns that define the row. These two are often used together, but there's no requirement that this be so.

What is the difference between primary key and identity in SQL?

IDENTITY, combined with a PRIMARY KEY or UNIQUE constraint, lets you provide a simple unique row identifier Primary key emphasizing on uniqueness and avoid duplication value for all records on the same column, while identity provides increasing numbers in a column without inserting data.

What is primary key in SQL Server?

SQL PRIMARY KEY Constraint The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

How to create a primary key constraint in SQL Server?

If you need to create a primary key in an existing table, see How to Add a Primary Key to an Existing Table in SQL Server. First I’ll create a test database: Now create a new table that includes a primary key constraint: This created a new table called Colors which has a primary key constraint on its ColorId column.

How to add a primary key to multiple columns in SQL?

ADD PRIMARY KEY (ID); To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: MySQL / SQL Server / Oracle / MS Access:


1 Answers

Simple change to syntax is all that is needed:

 create table ImagenesUsuario (    idImagen int not null identity(1,1) primary key  ) 

By explicitly using the "constraint" keyword, you can give the primary key constraint a particular name rather than depending on SQL Server to auto-assign a name:

 create table ImagenesUsuario (    idImagen int not null identity(1,1) constraint pk_ImagenesUsario primary key  ) 

Add the "CLUSTERED" keyword if that makes the most sense based on your use of the table (i.e., the balance of searches for a particular idImagen and amount of writing outweighs the benefits of clustering the table by some other index).

like image 199
richardtallent Avatar answered Oct 17 '22 20:10

richardtallent