Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIVERSITY OF ENGINNERING AND TECHNOLOGY,

TAXILA

Subject: DBS LAB


Assignment No # 01
Dated: 15/02/24 (Thursday)

Submitted By:
Wajeeha Islam
22-SE-18
(OMEGA)

Submitted To:

Ma’am Rabia

Department of Software Engineering,


UET TAXILA
Q:NO:01: What is ‘DOMAIN’ SQL

SQL 'DOMAIN' Statement:

Creating and managing databases can be a complex task, especially when dealing with
extensive data types and constraints. The SQL 'DOMAIN' statement acts as a solution to this
concern, providing a way to define a field with specific data type and constraint, which can be
reused throughout the database. This tutorial aims to provide a comprehensive understanding
of the SQL 'DOMAIN' statement, its syntax, usage, and benefits.

Understanding the 'DOMAIN' Statement:

In SQL, a DOMAIN is a named data type with a specified size, precision, and constraints. It
allows you to create a custom data type that can be used across multiple tables, ensuring data
integrity and consistency. It's like creating a template for a particular type of column.

Creating a DOMAIN

The syntax for creating a DOMAIN in SQL is as follows:

CREATE DOMAIN domain_name [AS] data_type

[DEFAULT expression]

[CHECK (condition)];

Here, 'domain_name' is the name of the domain, 'data_type' is the type of data the domain will
hold, 'DEFAULT expression' is an optional default value, and 'CHECK (condition)' is an
optional constraint that the data must meet.

Example of Creating a DOMAIN

CREATE DOMAIN Age AS INTEGER

DEFAULT 0

CHECK (VALUE >= 0 AND VALUE <= 120);

This statement creates a new DOMAIN called 'Age' that holds INTEGER values. The default
value is 0, and the CHECK constraint ensures that the age is between 0 and 120.

CREATE TABLE Employee (

EmployeeID INT PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),
Age INT CHECK (Age >= 18),

Salary DECIMAL(10, 2) CHECK (Salary >= 0)

);

In this example, EmployeeID is an integer, FirstName and LastName are variable character
strings, Age is an integer with a CHECK constraint ensuring it's 18 or older, and Salary is a
decimal with a CHECK constraint ensuring it's non-negative. These constraints help define the
domain for each column, specifying the valid range or conditions for the data.

CREATE DOMAIN PositiveIntDomain AS INTEGER CHECK (VALUE >= 0);

CREATE TABLE Employee (

EmployeeID PositiveIntDomain PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),

Age PositiveIntDomain CHECK (Age >= 18),

Salary DECIMAL(10, 2) CHECK (Salary >= 0)

);

Advantages of Using 'DOMAIN' Statements

Using DOMAIN statements in SQL provide several benefits:


• Consistency: By defining a DOMAIN, you ensure that all fields using this DOMAIN
have the same data type, size, and constraints, promoting data consistency.
• Data Integrity: The CHECK constraint in a DOMAIN enforces specific rules on the
data, which enhances data integrity.
• Efficiency: Instead of writing the same constraints for multiple fields, you can define
a DOMAIN once and reuse it, saving time and effort.

DOMAIN in Oracle SQL:


There are tow ways to insert domain in SQL:
1. Check constraint
2. User-defined
CHECK constraint:
1. Creating the Product Table with columns for product_id, product_name, and price.:
2. The price column has a check constraint (CHECK (price >= 0)) to ensure that the price
is non-negative.
3. There's a valid_price_range constraint that checks whether the price is within a valid
range

4. Inserting the correct values:


5. Inserting the Invalid Value:

6. Viewing Table:
User-defined:
1. Create Object Type:

Output:

2. Create Table Using User-Defined Type:

Conclusion

Understanding the SQL 'DOMAIN' statement and its usage is critical for any software
developer dealing with databases. It not only promotes data consistency and integrity but also
enhances efficiency.

You might also like