Sie sind auf Seite 1von 5

Command syntax of create

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 add command


ALTER TABLE table_name
ADD column_name columndefinition;
Ex.
ALTER TABLE customers
ADD customer_name
varchar2(45);

ALTER TABLE
customer
MODIFY
(
cust_name varchar2(100) not
null,
cust_hair_color varchar2(20)
)
;

Alter table drop column


command
alter table
table_name
drop column
col_name1; -- drop ONE column
ex.
alter table
cust_table
drop column
cust_sex;

alter table set unused


column command
alter table Personal set unused
column AGE;
rename a table

Alter table modified


command

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.

SQL> SELECT COUNT(*) FROM emp;

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')
)

Using primary key constraint


The PRIMARY KEY constraint uniquely identifies each record in a database
table.
Primary keys must contain UNIQUE values.
A primary key column cannot contain NULL values.
Most tables should have a primary key, and each table can have only ONE
primary key.
Ex.
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,

FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

Foreign key constraint


A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Let's illustrate the foreign key with an example. Look at the following two
tables:
Ex.
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)
Unique contraint
SQL> CREATE TABLE t (id1 NUMBER, id2 NUMBER);
Table created
SQL> ALTER TABLE t ADD CONSTRAINT u_t UNIQUE (id1, id2);
Table altered
SQL> INSERT INTO t VALUES (1, NULL);
1 row inserted
SQL> INSERT INTO t VALUES (1, NULL);
INSERT INTO t VALUES (1, NULL)
ORA-00001: unique constraint (VNZ.U_T) violated
SQL> /* you can insert two sets of NULL, NULL however */
SQL> INSERT INTO t VALUES (NULL, NULL);
1 row inserted
SQL> INSERT INTO t VALUES (NULL, NULL);
1 row inserted

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.

CREATE TABLE Persons


(
P_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

Not null constraint


By default, a column can hold NULL values. If you do not want a column to
have a NULL value, then you need to define such constraint on this column
specifying that NULL is now not allowed for that column.
A NULL is not the same as no data, rather, it represents unknown data.
Ex.

CREATE TABLE CUSTOMERS(


ID INT
NOT NULL,
NAME VARCHAR (20)
NOT NULL,
AGE INT
NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

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";

Das könnte Ihnen auch gefallen