Sep 14, 2009 Hello, I'm trying to create a primary key that will auto-generate. I have tried using a unique identifier and setting the default value/binding to newid but I still have to explicitly use newid when inserting rows or it will complain about a null id.
Jul 12, 2013 12:52 AMRagavanBLINK
Hi Manish,
try like this sample,
Personal detail:
create table personaldetail(id bigint identity primary key,pid nvarchar(20) unique,name nvarchar(25),addres nvarchar(100))
Occupation:
create table occupation(id bigint identity primary key,pid nvarchar(20) references personaldetail(pid),occupation nvarchar(25),dept nvarchar(25))
then generate the pid at code behind like,
public string generate_ID(){
int id=0;
string pid;
string pidquery = 'select top 1 SUBSTRING(pid,4,len(piid))as pid from personal detail order by pid desc';
// here 4 char denotes number after prefix string('HUA')
SqlCommand scmd = new SqlCommand(pidquery, con);
SqlDataAdapter adapter = new SqlDataAdapter(pidquery, con);
DataTable restable = new DataTable();
adapter.Fill(restable);
if (restable.Rows.Count > 0)
{
SqlDataReader reader = scmd.ExecuteReader();
reader.Read();
id= Convert.ToInt32(reader['pid']);
id= id+ 1;
pid = 'HUA' + Convert.ToString(id);
}
else // its for if no data in db
{
id= 1;
pid = 'HUA' + Convert.ToString(jid);
}
return pid;
}
after got id:
insert into both tables like:
1. insert into personaldetail('+pid+','+name+','+addr+')
Endnote x4 product key generator. 2.insert into occupation('+pid+','+occupation+','+dept+')
please let me know the status.
Thank you,
-->You can define a primary key in SQL Server by using SQL Server Management Studio or Transact-SQL. Creating a primary key automatically creates a corresponding unique clustered index, or a nonclustered index if specified as such.
A table can contain only one PRIMARY KEY constraint.
All columns defined within a PRIMARY KEY constraint must be defined as NOT NULL. If nullability is not specified, all columns participating in a PRIMARY KEY constraint have their nullability set to NOT NULL.
Creating a new table with a primary key requires CREATE TABLE permission in the database and ALTER permission on the schema in which the table is being created.
Creating a primary key in an existing table requires ALTER permission on the table.
Caution
If you want to redefine the primary key, any relationships to the existing primary key must be deleted before the new primary key can be created. A message will warn you that existing relationships will be automatically deleted as part of this process.
A primary key column is identified by a primary key symbol in its row selector.
If a primary key consists of more than one column, duplicate values are allowed in one column, but each combination of values from all the columns in the primary key must be unique.
If you define a compound key, the order of columns in the primary key matches the order of columns as shown in the table. However, you can change the order of columns after the primary key is created. For more information, see Modify Primary Keys.
The following example creates a primary key on the column TransactionID
in the AdventureWorks database.
The following example creates a table and defines a primary key on the column TransactionID
in the AdventureWorks database.
The following example creates a table and defines a primary key on the column CustomerID
and a clustered index on TransactionID
in the AdventureWorks database.