Suppose user name and phone number should be unique at the time of table define without using unique constraint and check constraint in sql server.i don't want to create sp for that.please suggest any otherway to prevent duplicate column in sql server r2 2008. Il
One correct way (and really the best way) is to use a unique constraint. It is unclear whether you want one or two constraints, based on your description. If you don't want a unique constraint, you should explain why not.
That is, if you want the pair to be unique:
alter table add constraint unq_t_username_phonenumber unique (username, phonenumber);
Or you want each one to be unique:
alter table add constraint unq_t_username unique (username);
alter table add constraint unq_t_phonenumber unique (phonenumber);
When you define the table, you can also do one-column unique constraints in-line:
create table . . . (
. . .,
UserName varchar(255) not null unique,
. . .
);
Almost equivalent to a unique constraint is a unique index:
create unique index unq_t_username on t(username);
The difference is that you can name the constraint. This name is handy when a violation occurs, because the constraint name (but not index name) is typically in the message.
Related
I have two tables, one of student and one of staff that look as such:
create table student (
id int not null primary key
)
create table staff (
id int not null primary key
)
I want the id in each to be unique. I know this isn't how it should be in production but I'm just trying to see why my check constraint doesn't work and I'm using a simpler example to explain.
I then alter the tables to include the check as follows:
alter table student add constraint not_staff check (id not in (select id from staff))
alter table staff add constraint not_student check (id not in (select id from student))
These checks seem to be invalid.
My question is whether we're allowed to have these kinds of SQL statements inside of a check constraint. If so, why is the above constraint invalid and how would I go about fixing it.
Thanks!
You can't use queries in a check constraint in Db2. Refer to the description of the CREATE TABLE statement.
CHECK (check-condition)
Defines a check constraint. The search-condition must be true or unknown for every row of the table.
search-condition
The search-condition has the following restrictions:
...
The search-condition cannot contain any of the following (SQLSTATE 42621):
Subqueries
The easiest way to achieve your goal is not to create constraints, but create a sequence and use it in before triggers on both tables.
I am importing data from an access table to SQL. I have a primary key is SQL "quoteID" which obviously doesn't allow duplicates but I'm looking to add that requirement to another field.
Can't seem to find where to set that? perhaps it has to do with the field type??
You can require that a column be unique using a unique constraint or unique index:
alter table t add constraint unq_t_col unique (col);
or:
create unique index unq_t_col on t(col);
Make the field a primary key of the table. Or put an index on that field that enforces unique values.
I have just inherited a database from another developer, and I have looked through the sys.objects table, filtering by constraints.
What does DF__role_sett__custo__4589517F mean - mainly the ID at the end of the string?
I know that DF_role_sett_custo means default constraint of role_setting_customer, but am not sure of the last ID 4589517f.
If you don't name a constraint when it is created, SQL Server will assign it a random name based on the table and column. It appends a random number so that it doesn't clash with existing constraint names.
In almost all cases, it is best to name a constraint when it is created. It's then easier to refer to the constraint by name in other T-SQL statements.
For instance, in the following create statement
CREATE TABLE dbo.some_table(
some_field INT NOT NULL DEFAULT(5)
);
The default constraint will be assigned a random name. In this statement:
CREATE TABLE dbo.some_table(
some_field INT NOT NULL CONSTRAINT DF_some_table_some_field DEFAULT(5)
);
The default constraint will have the name you assigned to it (i.e. DF_some_table_some_field).
How can I make a set of column to be unique key in sql server database?
for example: I have a database that have just one table by columns 1_book 2_page 3_line 4_word i want to search a word in some books and record this information .
where is the problem? if it find a words twice or more in a line it will save the same record to table.it is not important for me how many times a word is repeated in a line. i want if a word to be repeated once or more save the information.
is there any way to say every record should be unique?
searching a record in table before Inserting it to table is not reasonable .isn't it?
Just create a unique constraint for your table: (example for ms sql-server)
ALTER TABLE <yourtablename> ADD CONSTRAINT
UniqueEntries UNIQUE NONCLUSTERED
(
1_book, 2_page, 3_line, 4_word
)
If you do not want to get errors and simply ignore additional adds of the same word in the same line, you can extend the constraint with IGNORE_DUP_KEY = ON
Example:
ALTER TABLE <yourtablename> ADD CONSTRAINT
UniqueEntries UNIQUE NONCLUSTERED
(
1_book, 2_page, 3_line, 4_word
) WITH (IGNORE_DUP_KEY = ON)
Inserts with already existing records will then just be silently ignored.
You mean, you don't know how to create composite keys? Well:
alter table dbo.Words add constraint PK_Words primary key (
1_book, 2_page, 3_line, 4_word
);
And if you don't want key violations while adding data, use merge instead of insert (assuming your SQL Server version is 2008 or later).
In SQL Server 2005+ (I use both), does adding the UNIQUE constraint to a column automatically create an index, or should I still CREATE INDEX?
See this MSDN article:
The Database Engine automatically
creates a UNIQUE index to enforce the
uniqueness requirement of the UNIQUE
constraint.
If you do create an index, you'll end up with two indexes, as this example demonstrates:
create table TestTable (id int)
alter table TestTable add constraint unique_id unique (id)
create unique index ix_TestTable_id on TestTable (id)
select * from sys.indexes where [object_id] = object_id('TestTable')
This will display two unique indexes on TestTable; and the HEAP that represents the table itself.
Yes, it does.
In fact, you can even create a CLUSTERED UNIQUE CONSTRAINT:
ALTER TABLE mytable ADD CONSTRAINT UX_mytable_col1 UNIQUE CLUSTERED (col1)
, which will make the table to be clustered on col1.
Almost all databases create an index for UNIQUE CONSTRAINT, otherwise it would be very hard to maintain it.
Oracle doesn't even distinguish between UNIQUE CONSTRAINT and UNIQUE INDEX: one command is just a synonym for another.
The only difference in Oracle is that a UNIQUE INDEX should have a user-supplied name, while a UNIQUE CONSTRAINT may be created with a system-generated name:
ALTER TABLE mytable MODIFY col1 UNIQUE
This will create an index called SYS_CXXXXXX.
An index is created when you add a unique constraint:
Reference -- see the second paragraph.
When a UNIQUE constraint is added to
an existing column or columns in the
table, by default, the Database Engine
examines the existing data in the
columns to make sure all values are
unique. If a UNIQUE constraint is
added to a column that has duplicated
values, the Database Engine returns an
error and does not add the constraint.
The Database Engine automatically
creates a UNIQUE index to enforce the
uniqueness requirement of the UNIQUE
constraint. Therefore, if an attempt
to insert a duplicate row is made, the
Database Engine returns an error
message that states the UNIQUE
constraint has been violated and does
not add the row to the table. Unless a
clustered index is explicitly
specified, a unique, nonclustered
index is created by default to enforce
the UNIQUE constraint.