I have a table with the following fields:
searchID ( I have set this as a primary key )
SearchText nvarchar(MAX)
.......
and so on
I want to make SearchText also as an additional primary field. How should this be done? Is that a good procedure to make two primary columns for a table?
It's impossible to have more than one primary key on one table and store unique values.
Your primary key must be as short as possible.
If you need to keep unique data in other column, you can create unique key on this column:
CREATE TABLE dbo.Table
(
SearchText nvarchar(MAX)NOT NULL,
CONSTRAINT AK_SearchText UNIQUE(SearchText)
);
Or with management studio:
You can create composite key as below
create table myTable
(
SearchId int not null,
SearchText nvharchar not null
)
GO
-- Add Constraint
ALTER TABLE myTable
ADD CONSTRAINT pk_myConstraint PRIMARY KEY (SearchId ,SearchText)
GO
Related
I am creating a table Brands with the following schema :
UserId
CarId
Brand
The UserId references the id of an user in the user table
The CarId references the id of a car in the car table
The only query that I will use is a search bases on these 2 columns, to get the corresponding brand.
So my question was about the constraint part, as I am a beginner, I would like to know which type of constraint to use (index, primary key, clustered or non clustered, on each field or on the 2 fields together) to have my query the more optimized possible.
This is my script right now :
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Brands]') AND type in (N'U'))
BEGIN
CREATE TABLE [Brands]
(
UserId BIGINT NOT NULL
CONSTRAINT [FK_Brands_Users]
FOREIGN KEY REFERENCES [Users] (UserId),
CarId BIGINT NOT NULL
CONSTRAINT [FK_Brands_Cars]
FOREIGN KEY REFERENCES [Cars] (CarId),
Brand nvarchar(15),
);
END
GO
ALTER TABLE [Brands]
ADD CONSTRAINT [Unique_UserId_BrandId] UNIQUE(UserId, BrandId);
GO
I would create a primary key including both fields. When you define a primary key it automatically create a clustered index. Also your primary key has a unique constraint build in. Your table is now a heap the way you wrote it here above which is not good. You can additionally create an extra non-clustered index on CarId. Having an additional non-clustered index on UserId is not usefull I think. The column UserId can use the clustered index because it's the first field in the clustered index but I'm not sure about that.
I have the following three sample tables (simplified demo for purpose of question):
CREATE TABLE Teams
(
Id int IDENTITY(1,1) NOT NULL,
Name varchar(50) NOT NULL,
PRIMARY KEY (Id)
)
CREATE TABLE TeamGroups
(
Id int NOT NULL,
TeamId int NOT NULL,
PRIMARY KEY (Id,TeamId)
)
CREATE TABLE RoomBookings
(
Id int NOT NULL,
TeamGroupId int NOT NULL,
RoomId int NOT NULL,
PRIMARY KEY (Id)
)
and I have the following foreign key already set up:
ALTER TABLE TeamGroups WITH CHECK
ADD CONSTRAINT [FK_TeamGroups_Teams]
FOREIGN KEY (TeamId) REFERENCES Teams(Id)
The idea is that each Team can be in zero or more TeamGroups, and each TeamGroup can have zero or more RoomBookings
To reflect that, I want to add a foreign key from the RoomBookings table into the TeamGroups table.
I tried using the Relationships GUI in Management Studio to create the foreign key (primary key table: TeamGroups.ID, foreign key table: RoomBookings.TeamGroupId) but I get an error:
The columns in table 'TeamGroups' do not match an existing primary key
or UNIQUE constraint
I'm assuming it's because the TeamGroups table has a two-column primary key?
I don't really want to make a foreign key constraint from the TeamGroups table (eg, the key is present in the TeamGroups table), as the table will eventually be used by other tables (such as EquipmentBookings, GroupManagers, etc).
Any help?
If your primary key is made up from more than one columns, then all foregin keys also must have all those columns - there's no way around this.
But I don't understand why you'd get this error trying to link TeamGroups to Team based on the Team.Id column.... that should work just fine.
Try using this:
ALTER TABLE TeamGroups WITH CHECK
ADD CONSTRAINT [FK_TeamGroups_Teams]
FOREIGN KEY (TeamId) REFERENCES Teams(Id);
You had Teams (which is not a valid column in TeamGroups at all), and you had REFERENCES Teams.id which is wrong - it needs to be REFERNCES Teams(Id); (column in parenthesis - not the "dot" notation)
Update: from TeamGroups to RoomBookings - yes.... either use both columns from TeamGroups in your RoomBookings table - or what would stop you from making the TeamGroups.Id column an INT IDENTITY and then have the PK on just this one column?? Any good reason for that??
CREATE TABLE TeamGroups
(
TeamGroupId int NOT NULL,
TeamId int NOT NULL,
PRIMARY KEY (TeamGroupId)
)
ALTER TABLE dbo.RoomBookings
ADD CONSTRAINT FK_RoomBookings_TeamGroup
FOREIGN KEY TeamGroupId REFERENCES TeamGroups(TeamGroupId)
I have this table:
CREATE TABLE [dbo].[Question] (
[QuestionId] INT IDENTITY (1, 1) NOT NULL,
[Text] NVARCHAR (4000) NULL,
[QuestionUId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
);
I would like to create a foreign key linking QuestionUId in my other table AdminTestQuestion back to the QuestionUId in the Question table.
The referenced table '[dbo].[Question]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
Any advice would be much appreciated.
First of all: this is really NOT a computed column - it's just a regular column with a default constraint...
For a column in a table to be used for a foreign key reference, it must be either the primary key of that table, or it has to have a unique index on it.
So here, all you need to do is to add a unique index on your column
CREATE UNIQUE INDEX UIX_QuestionUid ON dbo.Question(QuestionUid)
and then you should be able to reference it as a foreign key.
I've looked at a few SQL posts already but i'm unsure of their answers.. eg: SQL Unique Key Syntax
I want a Cities table... I want to define my country table whereby the Name column is unique... Using this as the base, how do I define a Unique column?
CREATE TABLE [dbo].[Cities]
(
[CityID] INT NOT NULL PRIMARY KEY,
[Name] NCHAR(100) NULL
)
You can create a constraint on the table to do this:
ALTER TABLE <tablename> ADD CONSTRAINT
<constraintname> UNIQUE NONCLUSTERED
(
<columnname>
)
You can add the unique keyword:
CREATE TABLE [dbo].[Cities]
(
[CityID] INT NOT NULL PRIMARY KEY,
[Name] NCHAR(100) NULL UNIQUE
);
There are alternative methods. You can add also add a unique index:
create unique index cities_name on dbo.Cities(Name);
Or do it with a unique constraint:
CREATE TABLE [dbo].[Cities]
(
[CityID] INT NOT NULL PRIMARY KEY,
[Name] NCHAR(100) NULL,
constraint unique_name unique(Name)
);
I would like create a table and add to it a Primary Key.
As for my understanding MS SQL add a clustered Index on the Primary Key and will name it with a default name.
I would like to know if is possible create a table and ASSIGN a custom name for the index created by default or how can i change the default name after the table as been created.
Thanks!
Sure - you can define the PRIMARY KEY constraint in your CREATE TABLE statement.
This will generate the default PRIMARY KEY
CREATE TABLE dbo.Table
(ID INT IDENTITY PRIMARY KEY,
.......)
but you can totally define the name of the constraint, too:
CREATE TABLE dbo.Table2
(ID INT IDENTITY CONSTRAINT PK_Table2 PRIMARY KEY,
......)
When you create a Clustered Primary Key, you do´nt create a index, but a Table organized as index.
Clustered Primary key is default option when you create a table with primary key on SqlServer.
http://msdn.microsoft.com/en-us/library/aa933131(SQL.80).aspx