Is it possible to use unique on multiple columns?
like:
user_vote user_id
------------------
1 1
1 2
2 1
both unique
This must be possible
But:
user_vote user_id
1 2
1 2
This must not be possible
You can add a unique constraint on the column's combination:
ALTER TABLE my_table
ADD CONSTRAINT my_table_uq UNIQUE (user_vote, user_id)
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE uservotetable
(
user_vote int NOT NULL,
user_id int NOT NULL,
CONSTRAINT uservote UNIQUE (user_vote ,user_id)
);
and if you created your table before ..then you can use ALTER
ALTER TABLE uservotetable
ADD CONSTRAINT uservote UNIQUE (user_vote ,user_id)
this can be useful for you sql_unique
You need to define a composite unique constraint.
SQL Server,
One way to do that in SQL Server is by adding UNIQUE INDEX
ALTER TABLE table_name ADD UNIQUE INDEX (User_Vote, User_Id);
In Oracle,
ALTER TABLE table_name
ADD CONSTRAINT uc_1 UNIQUE (User_Vote, User_Id)
Related
I'm trying to create a sql table in net frame work in VS that has room and user id ,but I want that only one of each combination can exist in the table:
Want:
room --- user id
1 2
1 3
3 2
2 1
1 1
3 1
Dont want:
room --- user id
1 2
1 2
how can I make it so that only unique combination can be entered?
You can enforce uniqueness using a unique constraint or index:
create unique index unq_t_room_userid on t(room, user_id);
or:
alter table t add constraint unq_t_room_userid
unique (room, user_id);
Looks like you want Room, UserId to be the PRIMARY KEY on that table.
CREATE TABLE Occupancy
(
RoomId INT NOT NULL
,UserId INT NOT NULL
,CONSTRAINT PK_Occupancy PRIMARY KEY CLUSTERED (RoomId,UserId)
)
When you try to insert a duplicate value:
INSERT dbo.Occupancy (RoomId,UserId)
VALUES
(1,1)
,(1,2)
,(1,1)
GO
You will get a message like this:
Violation of PRIMARY KEY constraint 'PK_Occupancy'. Cannot insert
duplicate key in object 'dbo.Occupancy'. The duplicate key value is
(1, 1).
I have a table which has a father_id column and a serial_number column,
I want to create a unique constraint on the serial number by the father id.
Meaning rows that have the same father_id can't have the same serial_number, but rows that have a different father_id can have the same serial_number.
To rephrase the requirement, you want the combination of father_id and serial_number to be unique. Once the requirement is phrased like that, it's easier to translate to SQL:
ALTER TABLE mytable
ADD CONSTRAINT mytable_unq UNIQUE (father_id, serial_number);
This looks like a compound unique key. Here is one way to do it with a unique index:
create unique index myindex on mytable(father_id, serial_number);
Or in a create table statement, with a unique constraint:
create table mytable (
father_id int,
serial_number int,
constraint myconstraint unique (father_id, serial_number)
);
This question already has answers here:
Oracle SQL - Add Primary Key to table
(2 answers)
Closed 5 years ago.
this is my table.In this table i want to add a primary key column name "emp_id" as the first column .I don't know how to do it .So,can you please help me!
EMP_NAME EMP_POS SALARY GENDER
----------------- ----------------- -------------- ------
anand worker 10000 M
balu manager 50000 M
carl manager 50000 M
riya md 60000 F
prabhu owner 99999999 M
The old way of doing this is a multi-step process:
add the column which will be the primary key
update the column
enforce the primary key.
Something like this:
create sequence t23_id;
alter table t23 add id number;
update t23
set id = t23_id.nextval
;
alter table t23 add constraint t23_pk primary key (id);
In 12c Oracle added Identity columns (like SQL Server auto-incrementing columns). This reduces the number of steps to two:
alter table t23i add id number GENERATED ALWAYS AS IDENTITY primary key;
alter table t23i add constraint t23i_pk primary key (id);
Unfortunately it can't be done in one step. This ...
alter table t23i add id number GENERATED ALWAYS AS IDENTITY primary key;
...hurls ...
ORA-01758: table must be empty to add mandatory (NOT NULL) column
Livesql demo
Introduce identity column:
see http://sql-plsql.blogspot.sg/2014/11/add-identity-column-to-table.html
Reorder the columns:
see http://www.dba-oracle.com/t_change_column_order_within_oracle_table.htm
Hope these references help.
I have a script written for Oracle databases that I'm converting to work in SQL Server and I have two questions about a specific section of code.
In Oracle script I have this code:
CREATE UNIQUE INDEX "PK_PORTALROLES" ON "PORTAL_ROLE" ("ROLE_NAME");
ALTER TABLE "PORTAL_ROLE" ADD CONSTRAINT "PK_PORTALROLES"
PRIMARY KEY ("ROLE_NAME") USING INDEX ENABLE;
Question (1)
From the code above what is the USING INDEX command in the ALTER TABLE line doing? Is it assigning the UNIQUE INDEX created on the first line to the newly created CONSTRAINT or is the CONSTRAINT getting a new UNIQUE INDEX to use when it is created?
Question (2)
To duplicate this in SQL Server, I commented out the CREATE UNIQUE INDEX line like this:
--CREATE UNIQUE INDEX "PK_PORTALROLES" ON "PORTAL_ROLE" ("ROLE_NAME");
And then replaced the ALTER TABLE line with this:
ALTER TABLE "PORTAL_ROLE" ADD CONSTRAINT "PK_PORTALROLES" PRIMARY KEY ("ROLE_NAME");
I understand that when a PRIMARY KEY CONSTRAINT is created in SQL Server a UNIQUE INDEX is automatically made. So is the one line of SQL Server code directly above doing the same thing as the two lines of Oracle code above?
EDIT
One final question. Is there a way in Oracle and SQL Server to assign an existing INDEX to a CONSTRAINT?
In Oracle
If there are more than one indexes on the column on which you want to add PK constraint, we can selectively choose the index to be assoicated with the PK using “USING INDEX“. This clause can be used while:
Adding the PK constraint for the first time (using “ALTER TABLE” command).
CREATE TABLE tbl_test ( col_1 NUMBER,
col_2 NUMBER,
col_3 NUMBER);
CREATE INDEX idx_col_1_2 ON tbl_test(col_1, col_2);
CREATE INDEX idx_col_1_3 ON tbl_test(col_1, col_3);
CREATE UNIQUE INDEX idx_col_1 ON tbl_test(col_1);
-- Forcing oracle to use the unique index "IDX_COL_1"
ALTER TABLE tbl_test ADD CONSTRAINT tbl_test_pk PRIMARY KEY(col_1)
USING INDEX idx_col_1;
SELECT constraint_name, constraint_type, index_name
FROM user_constraints
WHERE table_name = 'TBL_TEST';
-- CONSTRAINT_NAME | CONSTRAINT_TYPE | INDEX_NAME
-- TBL_TEST_PK | P | IDX_COL_1
What if? If you don't use the USING INDEX clause
ALTER TABLE tbl_test ADD CONSTRAINT tbl_test_pk PRIMARY KEY(col_1);
-- Although an unique index exists, oracle has picked up the first index
SELECT constraint_name, constraint_type, index_name
FROM user_constraints
WHERE table_name = 'TBL_TEST';
-- CONSTRAINT_NAME | CONSTRAINT_TYPE | INDEX_NAME
-- TBL_TEST_PK | P | IDX_COL_1_2
That shows The index associated with the PK constraint needn’t be unique.
But in SQLServer implicitly CLUSTERED INDEX will be created when primary key defined on any column
So to your last question. In Oracle you can assign index which we created can be assigned to constraints which we will create in future.
In SQLServer i guess it is not possible.
In Oracle, you can do
alter table PORTAL_ROLE add constraint pk_portalroles primary key('ROLE_NAME');
And it will create unique index for you. But when you drop or disable(!) constraint, it will drop that index. To avoid that (or to give index some custom name, or to use non-unique index, etc), it is usually done in 2 steps as in your code (as recommended by ORACLE).
Q1: constraint will be using existing index.
Q2: Yes, it will be enough for SQL Server.
When you specify a unique constraint on one or more columns, Oracle implicitly creates an index on the unique key. If you are defining uniqueness for purposes of query performance, then Oracle recommends that you instead create the unique index explicitly using a CREATE UNIQUE INDEX statement. You can also use the CREATE UNIQUE INDEX statement to create a unique function-based index that defines a conditional unique constraint.
EmpID DeptID
1 1
1 2
2 1
3 2
4 5
5 2
1 1
2 1
I would like to have a constraint that will make sure that the pair of field is always unique ,such data as last two shown in the example should not be insert-able into the table .in the above table
please note that last two rows are duplicates ,I would like to prevent such data from occuring .
How do I achieve this in sqlserver 2005.Thanks
ALTER TABLE <YourTable, sysname, Emp>
ADD CONSTRAINT <YourConstraintName, sysname, uix>
UNIQUE NONCLUSTERED (EmpID,DeptID)
(Paste into SSMS and use (CTRL + Shift + M))
Or to do this at table creation and as it sounds as though there is no alternative key use.
CREATE TABLE EMPLOYEE_DEPARTMENT(
EmpID int NOT NULL REFERENCES EMPLOYEE(EmpID),
DeptID int NOT NULL REFERENCES DEPARTMENT(DeptID),
CONSTRAINT PK_EMPLOYEE_DEPARTMENT PRIMARY KEY CLUSTERED (EmpID ASC,DeptID ASC)
)
After you've gone through and removed the duplicates, run the following (substituting appropriate names)
ALTER TABLE table ADD CONSTRAINT UQ_EmpID_DeptID UNIQUE (EmpID,DeptID)
Or when creating your table:
CREATE TABLE T1 (
EmpID int not null,
DeptID int not null,
/* Other Columns */
constraint PK_T1 PRIMARY KEY (EmpID,DeptID)
)
(May as well make it the primary key, unless you've got another one in the table)
ALTER TABLE dbo.YOURTABLE ADD CONSTRAINT IX_YOURTABLE UNIQUE NONCLUSTERED (EmpID, DeptID)
select empID, deptID from table
group by empID, deptID
EDIT:
If you are saying this data must be unique in table itself, i.e. The insertion of duplicates should not be allowed, then you need to define a composite key (empID, deptID) on this table.
alter table <tablename> add constraint <compositekeyname> primary key (empID, deptID)