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

To create a table with constraints in Oracle, you can use the following query as an example:

Sql Copy code

CREATE TABLE table_name ( column1 data_type constraints, column2 data_type constraints, ...
columnN data_type constraints );

In this query, you need to replace table_name with the desired name for your table and column1,
column2, ..., columnN with the actual column names you want to create. data_type should be replaced
with the appropriate data type for each column.

Now, let's look at some common constraints that you can include:

1. Primary Key Constraint:

sqlCopy code

CREATE TABLE table_name ( column1 data_type PRIMARY KEY, ... );

2. Unique Constraint:

sqlCopy code

CREATE TABLE table_name ( column1 data_type UNIQUE, ... );

3. Not Null Constraint:

Sql Copy code

CREATE TABLE table_name ( column1 data_type NOT NULL, ... );

4. Foreign Key Constraint:

Sql Copy code

CREATE TABLE table_name1 ( column1 data_type PRIMARY KEY, ... ); CREATE TABLE table_name2 (
column1 data_type, column2 data_type, ... CONSTRAINT fk_name FOREIGN KEY (column1) REFERENCES
table_name1 (column1) );

In this example, table_name1 is the referenced table, and table_name2 is the table containing the
foreign key.

These are just a few examples of constraints you can use. There are more options available, such as
check constraints, unique key constraints, etc. You can mix and match these constraints based on your
requirements.

Remember to replace data_type with the appropriate data type for each column and provide
meaningful names for the constraints (PRIMARY KEY, UNIQUE, NOT NULL, fk_name, etc.).

You might also like