April 30, 2012 at 4:07 am
hi Guy i need ur help please i create a table Auto with column name and Immatriculation
i try to make that when i write for example in my column name 'Ferrari ' that when somebody else write again ferrari it will not accept because ferrari always exist in my table. i try this.
create table auto
(pid int not null
,name varchar (50)
,Immatriculation varchar(50)
,CONSTRAINT name UNIQUE(name ))
create table auto
(pid int not null
,name varchar (50)
,Immatriculation varchar(50)
,CHECK (name <> name))
i receive a fault that it is a conflict. Please can someone help me??
April 30, 2012 at 4:35 am
From what I understand...you want the Name field in your table to be Unique....right??
You should add a UNIQUE Constraint to the field "Name".
April 30, 2012 at 4:36 am
April 30, 2012 at 4:58 am
inf1154 (4/30/2012)
hi Guy i need ur help please i create a table Auto with column name and Immatriculationi try to make that when i write for example in my column name 'Ferrari ' that when somebody else write again ferrari it will not accept because ferrari always exist in my table. i try this.
create table auto
(pid int not null
,name varchar (50)
,Immatriculation varchar(50)
,CONSTRAINT name UNIQUE(name ))
That code will do exactly what you want, though you should avoid reserved words as object names.
CREATE TABLE automobile (
pid INT NOT NULL ,
name VARCHAR(50) ,
Immatriculation VARCHAR(50) ,
CONSTRAINT uq_automobile_name UNIQUE ( name )
)
INSERT INTO automobile
( pid, name, Immatriculation )
VALUES ( 1, 'Ferrari', 'abc' )
INSERT INTO automobile
( pid, name, Immatriculation )
VALUES ( 2, 'Ferrari', 'def' )
The second insert fails with
Msg 2627, Level 14, State 1, Line 4
Violation of UNIQUE KEY constraint 'uq_automobile_name'. Cannot insert duplicate key in object 'dbo.automobile'. The duplicate key value is (Ferrari).
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 30, 2012 at 5:20 am
thanks Guy it walk i use
UNIQUE NONCLUSTERED and it walk
thx for ur help.
April 30, 2012 at 11:00 pm
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply