to create a table

4
To create a new database: CREATE DATABASE baru; To create a table, you use the aptly named CREATE TABLE command. For example: CREATE TABLE my_first_table ( first_column text, second_column integer ); This creates a table named my_first_table with two columns. The first column is named first_column and has a data type of text; the second column has the name second_column and the type integer. CREATE TABLE products ( product_no integer, name text, price numeric ); If you no longer need a table, you can remove it using the DROP TABLE command. For example: DROP TABLE my_first_table; DROP TABLE products; All these actions are performed using the ALTER TABLE command, 5.5.1. Adding a Column To add a column, use a command like:

Upload: muhammadwahyutp

Post on 04-Oct-2015

215 views

Category:

Documents


2 download

DESCRIPTION

Untuk membuat tabel

TRANSCRIPT

To create a new database:CREATE DATABASE baru;To create a table, you use the aptly namedCREATE TABLEcommand. For example:CREATE TABLE my_first_table ( first_column text, second_column integer );This creates a table namedmy_first_tablewith two columns. The first column is namedfirst_columnand has a data type oftext; the second column has the namesecond_columnand the typeinteger.CREATE TABLE products ( product_no integer, name text, price numeric );If you no longer need a table, you can remove it using theDROP TABLEcommand. For example:DROP TABLE my_first_table;DROP TABLE products;All these actions are performed using theALTER TABLEcommand,5.5.1. Adding a ColumnTo add a column, use a command like:ALTER TABLE products ADD COLUMN description text;The new column is initially filled with whatever default value is given (null if you don't specify aDEFAULTclause).You can also define constraints on the column at the same time, using the usual syntax:ALTER TABLE products ADD COLUMN description text CHECK (description '');5.5.2. Removing a ColumnTo remove a column, use a command like:ALTER TABLE products DROP COLUMN description;Whatever data was in the column disappears. Table constraints involving the column are dropped, too. You can authorize dropping everything that depends on the column by addingCASCADE:ALTER TABLE products DROP COLUMN description CASCADE;5.5.3. Adding a ConstraintTo add a constraint, the table constraint syntax is used. For example:ALTER TABLE products ADD CHECK (name '');ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;To add a not-null constraint, which cannot be written as a table constraint, use this syntax:ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;The constraint will be checked immediately, so the table data must satisfy the constraint before it can be added.5.5.4. Removing a ConstraintTo remove a constraint you need to know its name. If you gave it a name then that's easy. Then the command is:ALTER TABLE products DROP CONSTRAINT some_name;This works the same for all constraint types except not-null constraints. To drop a not null constraint use:ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;5.5.5. Changing a Column's Default ValueTo set a new default for a column, use a command like:ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;Note that this doesn't affect any existing rows in the table, it just changes the default for futureINSERTcommands.To remove any default value, use:ALTER TABLE products ALTER COLUMN price DROP DEFAULT;5.5.6. Changing a Column's Data TypeTo convert a column to a different data type, use a command like:ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);5.5.7. Renaming a ColumnTo rename a column:ALTER TABLE products RENAME COLUMN product_no TO product_number;5.5.8. Renaming a TableTo rename a table:ALTER TABLE products RENAME TO items;