May 29th, 2009

Data Definition Language, DROP TABLE

Posted by admin in Q. Data Definition Language

ou use a DROP TABLE command to delete a table.
Format:

          DROP TABLE table_name;

> If you DROP a table, it means that you lose all of the table’s structure and parameters.

> Some RDBMS systems will not allow you to DROP a table referenced by a FOREIGN KEY until the FOREIGN KEY constraint or the table containing the FOREIGN KEY constraint has been removed.

> Dropping a table does not remove a view, but a view can not function without a base table.

> A table can be empty (no data) without being dropped.

May 27th, 2009

Data Definition Language, ALTER TABLE

Posted by admin in Q. Data Definition Language

You use an ALTER TABLE comand to change names, data types, defaults, and constraints.Format:

  ALTER TABLE table_name
      {[ADD [COLUMN] column_name data_type [constraints]         Add a new column
     | [ALTER [COLUMN] column_name SET DEFAULT default_value]    Change a column default
     | [ALTER [COLUMN] column_name DROP DEFAULT]                 Remove a column default
     | [DROP [COLUMN] column_name [RESTRICT | CASCADE]]          Remove a column
     | [ADD CONSTRAINT table_constraint]                         Create a table constraint
     | [DROP CONSTRAINT constraint_name [RESTRICT | CASCADE]     Remove a table constraint
     | [RENAME TO new_table_name]};                              Rename a table

> You can create, change, or remove a table or column constraint, such as a primary key, foreign key, unique, or check constraint. ADD creates an entry, ALTER changes an entry, and DROP removes an entry.

> The format of the ALTER statement varies greatly from one RDBMS system to another. Consult your RDBMS documentation for the proper format and other parameters.

> DB2 will not allow you to DROP a column.

> DB2, MySQL, and Oracle don’t use ALTER, but use RENAME TABLE old_table_name TO new_table_name.

> PostgreSQL uses ALTER TABLE old_table_name TO new_table_name.

May 25th, 2009

Data Definition Language, CREATE TABLE

Posted by admin in Q. Data Definition Language

This starts Data Definition Language (DDL) commands. See Table Creation and SQL is DML, DDL, & DCL.
While a DataBase Administrator (DBA) will normally perform the DDL and DCL functions, you need to know how they work so that you can convey your needs to the DBA.As previously described, a database table is a two-dimensional array of columns (or attributes) and rows (or records).
You use a CREATE TABLE comand to specify the name and data type of each column.

Format:

          CREATE TABLE table_name (
                 column_name1 data_type1 [col_constraint1],
                 column_name2 data_type2 [col_constraint2],
                 ...
                 [CONSTRAINT constraint_name1 table_constraint1,]
                 [CONSTRAINT constraint_name2 table_constraint2,]
                 ...
                 );

Once the table is created, you can start loading it with data. This is done with the INSERT statement described earlier. ALTER TABLE changes the attributes of the table and DROP TABLE will remove the table definition.

As you design your tables, you will need to go through table normalization which is a way of structuring your tables so that updates do not introduce abnormalities.

The column_name must be a valid identifier according to the rules of each RDBMS.
The specific data_type associated with the column name may contain a length according to the RDBMS.
The constraint specifies constraints on the data such as NOT NULL and DEFAULT.

Next Page »