|
|
FULL A - Z Oracle Function Library (formerly named "Morgan's Library")
Term: CONSTRAINT
Definition: CREATE TABLE products ( Notice in the example above that we've also given the constraint a name of "positive_cost". Giving constraints names helps identify what they do and makes your code more readable and maintainable. You can also create constraints that include multiple columns and conditions, if you want. This example checks to make sure that the cost is above 0, and it also checks to make sure that the discounted cost is less than the regular cost: CREATE TABLE products ( Foreign Key Constraint A foreign key is a constraint between two tables in a database The foreign key identifies a column (or set of columns) in one table (the "referencing" table) that refers to set of columns in another table (the "referenced" table). The referencing and referenced table may be the same table, that is, the foreign key refers back to the same table. Such a foreign key is known in ANSII SQL as a self-referencing or recursive foreign key. A table may have multiple foreign keys, and each foreign key can (and often does) have a different referenced table. Each foreign key constraint is enforced independently by the database system. Example Syntax: Foreign keys are normally defined as part of the CREATE TABLE SQL statement: CREATE TABLE Sales( Foreign keys can, however, be added to an existing table using the ALTER TABLE command: ALTER TABLE Sales Unique Key Constraint The UNIQUE constraint ensures that all values in a specified column are distinct, that is, the column cannot contain two identical instances of any value. Attempting to insert a duplicate value into a UNIQUE column will violate the UNIQUE constraint and result in an error. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. Note that a PRIMARY KEY constraint automatically includes a UNIQUE constraint. Example Syntax: CREATE TABLE users Primary Key Constraint A Primary Key is used to uniquely identify each row in a table. It acts to enforce row-level integrity of the table, meaning that it ensures that there are no duplicate records in a table. A primary key can be part of the record itself (such as Social Security Number) or it can be an independent field used only to identify the record itself. A primary key can also be composed of multiple fields on a table. When multiple fields are used as a primary key, it is referred to as a composite primary key. Primary keys can be created when the table itself is created or added to an existing table structure using the ALTER TABLE statement. Example Syntax: CREATE TABLE users ( You don't have to specify the primary key at the same time you are creating the column. You may also add it at the end of the table, as shown in this example: CREATE TABLE users ( Related Links: Related Code Snippets:
|
Home | Search | Code Library | Sponsors | Privacy | Terms of Use | Contact Us | © 2003 - 2024 psoug.org |