How to create primary key by using two columns in SQL Server - sql-server

I need to create table with two columns. These columns have to be created with primary key. For example Column 1 name ID and column 2 name Name. ID is auto incremented. When data is inserted into the table, all names has to be different. How can I do that? Please help me.

To create the primary key:
ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT PK_YourTable
PRIMARY KEY(ID, Name)
and to make sure the Name is unique:
ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT UNIQUE_Name UNIQUE(Name)

Related

Composite primary key in sql server

I am trying to create a composite primary key in Sql server.
The syntax I have tried is:
create table Installment_details
(
constraint P_key primary key(Account_No,month),
Account_No int not null,
foreign key (Account_No) references Account_details(Account_no) on delete cascade,
Month char(15) not null,
D#te date,
Receipt_no varchar(15),
Amount_received int,
Amount_left int,
Amount_receiver char(50),
)
As far as I know it should create column with column name P_key for primary key but whenever I make a entry in table it doesn't show this column.
You are confused about the terms you're using. It's not the same a Primary Key and a Column. For example, you're creating a Primary Key based on two existing columns, and the name P_Key it's the name of the Primary Key, which is the way SQL SERVER (in this case) can identify a row in the Table (it cannot be two rows with the same values on those two columns).
I hope this clarifies a little bit the issue.
I think you are getting it wrong P_key in your code is constraint's name not a column name.
Also composite key is not a column, it is used when you don't have a column with unique values. So you take combination of two or more column as primary key so that we can uniquely identify a row.

How to add another primary key in SQL Server

I have a SQL Server table that has primary keys
CompanyID
ClientID
ReportName
I need to add a column that is an additional key and an incremented ID.
alter table Exports
add id int identity(1,1)
How do I write the statement to make this a key as well?
You can have only 1 primary key or identity column in each table in SQL Server.
If you want to have one more column as primary key, then you may use the combination of the columns as the primary key.
Or you can set the constraints UNIQUE and NOT NULL to the columns so that they will act same as a primary key (but these columns can't be referred as foreign keys)
In case of auto incremental columns (identity), you can only have 1 identity column per table. It can also be set as a primary key. Otherwise, if you need multiple columns as auto-increment, then probably you can use a calculated column, a trigger or a sequence object from which you may fetch the values for each record

T-SQL inserting data into table with foreign key

I'm using SQL Server 2014 to create and insert data into tables and came along a problem when it comes to populating a table with a foreign key constraint in it. I have a table user and and a table city which were created beforehand.
I used code to alter the user table to include a cityId foreign key from table city with this code:
ALTER TABLE [Schema].[user]
ADD cityId UNIQUEIDENTIFIER NOT NULL
CONSTRAINT usr_cid_fk
FOREIGN KEY (cityId) REFERENCES [Schema].[city] (cityId);
GO
Basically I modified the user table by adding a field called cityId which i made foreign key. now the problem is that when inserting data, in the first line
INSERT INTO [Schema].[user](name, surname, dob, gender, .. )
cityId cannot be found to be mapped. Dunno why it ain't showing. In the design view it is listed as a foreign key so there should be no problems.
Thanks a lot
try :
ALTER TABLE [Schema].[user]
ADD cityId NUMBER NOT NULL
CONSTRAINT usr_cid_fk
FOREIGN KEY (cityId) REFERENCES [Schema].[city] (cityId);
Note :
For ADD cityId UNIQUEIDENTIFIER NOT NULL
By the SQL standard, a foreign key must reference either the primary key or a unique key of the parent table. If the primary key has multiple columns, the foreign key must have the same number and order of columns. Therefore the foreign key references a unique row in the parent table; there can be no duplicates.

SQL server - how does adding identity column in existing table works internally?

I have a table with 100 rows, but my primary key column is not with identity property.(but values of this column seems like identity column and sequential: 1-100)
Now i tried to add new identity column with below code
alter table tableA add new_Iden_col bigint identity(1,1)
but i find that the new column identity values are not in sorted order as per my primary key column.
my output of old Primary key col and new inserted identity column
So can someone explain me why the newly added column is not same as my primary key column even though my old column values are populated naturally in same way and not dynamically sorted for output.
Thanks in advance. :)
The IDENTITY function can be used to create a new IDENTITY column and assign incremental values in the sequence specified by the ORDER BY clause of SELECT...INTO. Below is an example creates a new IDENTITY column for use as a new primary key from the old primary key column order.
SELECT
IDENTITY(bigint, 1,1) AS new_Ident_col
, OtherData
INTO FROM dbo.TableA_New
FROM dbo.TableA
ORDER BY old_PK_col;
DROP TABLE dbo.TableA;
EXEC sp_rename 'dbo.TableA_New', 'TableA';
ALTER TABLE dbo.TableA ADD CONSTRAINT PK_TableA PRIMARY KEY(new_Ident_col);

foreign keys in sql server 2005

I having trouble creating a foreign key in sql 2005.
my primary key table has a primary key that spans 2 columns.
I want to create my foreign key so it references a column in my primary table but I want to specify a static value for the second column - is this possible?
No, you cannot do this - if you reference a parent table from a child table, you have to reference all columns of the primary key on the parent table:
ALTER TABLE dbo.ChildTable
ADD CONSTRAINT FK_ChildTable_ParentTable
FOREIGN KEY (col1, col2) REFERENCES dbo.ParentTable(pkCol1, pkCol2)
You cannot suddenly introduce for "static" value into your reference.
What you could do on the child table would be to put a CHECK CONSTRAINT on that second column to make it a "static" value - but then you can never have any other value in that column:
ALTER TABLE dbo.ChildTable
ADD CONSTRAINT chk_col2_Static CHECK (col2 = 4)
Is that what you are looking for??

Resources