1. What is a UNIQUE constraint in Oracle?
A UNIQUE constraint ensures that all values in a specified column (or combination of columns) are distinct across all rows in the table. It prevents duplicate non-NULL values, but multiple NULL values are allowed.
2. Can a UNIQUE constraint allow NULL values?
Yes, a UNIQUE constraint allows multiple NULL values. In Oracle, NULL is considered a special marker and is not treated as a duplicate of another NULL value. Therefore, you can have multiple rows with NULL in a column with a UNIQUE constraint.
3. How do I define a UNIQUE constraint in Oracle?
You can define the UNIQUE constraint during table creation or add it to an existing table.
- During table creation:
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
email VARCHAR2(100) UNIQUE
);
- Adding to an existing table:
ALTER TABLE employees
ADD CONSTRAINT unique_email UNIQUE (email);
4. What is the difference between UNIQUE and PRIMARY KEY constraints?
- PRIMARY KEY: Ensures that values are both unique and not NULL. A table can have only one PRIMARY KEY constraint.
- UNIQUE: Ensures values are unique but allows NULL values. A table can have multiple UNIQUE constraints.
5. Can I have multiple UNIQUE constraints in one table?
Yes, a table can have multiple UNIQUE constraints. You can apply UNIQUE to different columns or combinations of columns, as long as they ensure uniqueness.
6. Can a column with a UNIQUE constraint have duplicate values?
No, a column with a UNIQUE constraint cannot have duplicate non-NULL values. If you try to insert a duplicate value into that column, Oracle will raise a constraint violation error.
7. How does Oracle handle NULL values in a UNIQUE column?
Oracle allows multiple NULL values in a column with a UNIQUE constraint. Since NULL is not considered equal to another NULL, multiple rows with NULL in the UNIQUE column are allowed.
8. Can I create a UNIQUE constraint on multiple columns (composite key)?
Yes, you can create a UNIQUE constraint on a combination of columns, ensuring that the combination of values across the specified columns is unique.
Example:
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER,
product_id NUMBER,
CONSTRAINT unique_customer_product UNIQUE (customer_id, product_id)
);
9. What happens if I insert a duplicate value in a UNIQUE column?
If you attempt to insert a duplicate value (that is not NULL) into a column with a UNIQUE constraint, Oracle will raise an error, and the insertion will fail.
10. How does a UNIQUE constraint affect performance?
- Insert Performance: The UNIQUE constraint can slightly affect insert performance because Oracle must check for duplicates before inserting a row.
- Indexing: Oracle automatically creates a unique index on the column(s) defined with a UNIQUE constraint. This helps with query performance but adds overhead on INSERT, UPDATE, and DELETE operations.
11. How can I drop a UNIQUE constraint in Oracle?
To drop a UNIQUE constraint, you can use the ALTER TABLE statement:
ALTER TABLE employees
DROP CONSTRAINT unique_email;
12. Can I have both UNIQUE and PRIMARY KEY constraints on the same column?
No, a column can only have one PRIMARY KEY constraint. However, a column with a PRIMARY KEY automatically enforces uniqueness, so it does not need a separate UNIQUE constraint.
13. Can I create a UNIQUE constraint on a column with existing duplicate data?
No, you cannot apply a UNIQUE constraint to a column that already contains duplicate non-NULL values. You must first remove or update the duplicate rows before applying the constraint.
14. What happens if I try to insert a NULL value in a column with a UNIQUE constraint?
You can insert NULL values in a column with a UNIQUE constraint. Since NULL is not considered a duplicate of another NULL, multiple rows with NULL in the UNIQUE column are allowed.
15. Can a UNIQUE constraint be created without an index?
No, when you define a UNIQUE constraint, Oracle automatically creates a unique index on the column(s). This index ensures that the uniqueness rule is enforced efficiently.
16. Can I modify a UNIQUE constraint after it is applied?
Yes, you can modify a UNIQUE constraint, but you might need to drop and recreate it if you want to change the columns involved. Use the ALTER TABLE statement to modify constraints.
17. What is a UNIQUE constraint used for?
The UNIQUE constraint is used to ensure that a column (or combination of columns) does not have duplicate values, which is essential for maintaining data integrity in a database. It's commonly used for enforcing the uniqueness of fields such as email addresses, usernames, or product codes.
No comments:
Post a Comment