Command Syntax of Create Table
Command Syntax of Create Table
table
CREATE TABLE customers
( customer_id number(10) NOT
NULL,
customer_name varchar2(50) NOT
NULL,
city varchar2(50)
);
Ex.
Modifying existing table
CREATE TABLE customers
( customer_id number(10) NOT
NULL,
customer_name varchar2(50) NOT
NULL,
city varchar2(50),
CONSTRAINT customers_pk
PRIMARY KEY (customer_id)
);
ALTER TABLE
customer
MODIFY
(
cust_name varchar2(100) not
null,
cust_hair_color varchar2(20)
)
;
alter table
table_name
rename to
new_table_name;
alter table
table_name
modify
column_name datatype;
alter table
customer
rename to
old_customer;
ex.
truncating a table
Use the TRUNCATE TABLE statement to remove all rows from a table. By
default, Oracle Database also performs the following tasks:
Deallocates all space used by the removed rows except that specified by the
MINEXTENTS storage parameter
Sets the NEXT storage parameter to the size of the last extent removed from
the segment by the truncation process
Removing rows with the TRUNCATE TABLE statement can be more efficient
than dropping and re-creating a table. Dropping and re-creating a table
invalidates dependent objects of the table, requires you to regrant object
privileges on the table, and requires you to re-create the indexes, integrity
constraints, and triggers on the table and respecify its storage parameters.
Truncating has none of these effects.
Removing rows with the TRUNCATE TABLE statement can be faster than
removing all rows with the DELETE statement, especially if the table has
numerous triggers, indexes, and other dependencies.
Ex.
SQL>TRUNCATE TABLE emp;
Table truncated.
SQL> SELECT COUNT(*) FROM emp;
Delating a table
The DELETE command is used to remove rows from a table. A WHERE clause
can be used to only remove some rows. If no WHERE condition is specified,
all rows will be removed. After performing a DELETE operation you need to
COMMIT or ROLLBACK the transaction to make the change permanent or to
undo it. Note that this operation will cause all DELETE triggers on the table to
fire.
Ex.
SQL> DELETE FROM emp WHERE job = 'CLERK';
4 rows deleted.
SQL> COMMIT;
Commit complete.
Constraint
Constraints are the rules enforced on data columns on table. These are used
to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the database.
Creating constraint
--- creating table constraint at the column level
Column Level Constraints.
In this type the constraint is checked when the value of the column changed.
If we consider the previous example of check constraint a little change to the
code definition will make the constraint be checked on the column not on the
row.
Ex.
CREATE TABLE [COLUMNLEVEL]
(
[ID] INT PRIMARY KEY,
[STARTDATE] DATE NOT NULL,
[ENDDATE] DATE NOT NULL,
[CHECKED] DATE NOT NULL,
CONSTRAINT COLUMNLEVELCONSTRIANT CHECK( [CHECKED] > '2012-0101')
)
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
Check constraint
The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a single column it allows only certain
values for this column.
If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.
Ex.
Dropping constraints
Constraints can be placed on a table to limit the type of data that can go into
a table. Since we can specify constraints on a table, there needs to be a way
to remove this constraint as well. In SQL, this is done via the ALTER TABLE
statement.
The SQL syntax to remove a constraint from a table is,
Ex.
ALTER TABLE "table_name"
DROP [CONSTRAINT|INDEX] "CONSTRAINT_NAME";