Material

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Creating and Managing Table

Table
Basic unit of storage; composed of rows and columns

Table Structures
Tables can be created at any time, even while users are using
the database.
You do not need to specify the size of any table. The size is
ultimately defined by the amount of space allocated to the
database as a whole. It is important, however, to estimate
how much space a table will use over time.

Naming Rules
- Table names and column names:
Must begin with a letter
Must be 130 characters long
Must contain only AZ, az, 09, _, $, and #
Must not duplicate the name of another object owned by
the same user
Must not be an Oracle server reserved word

The CREATE TABLE Statement


Create tables to store data by executing the SQL CREATE
TABLE statement. This statement is one of the data definition
language (DDL) statements that are covered in subsequent
lessons. DDL statements are a subset of SQL statements used
to create, modify, or remove database structures. These
statements have an immediate effect on the database, and
they also record information in the data dictionary.

CREATE TABLE states


(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
state CHAR(25),
population INT(9));

This will create a table named "states" with three fields:


population.

id, state,

and

The INT command will make the id field contain only numbers (integers).
The NOT

NULL

command makes sure that the id field cannot be left blank.

The PRIMARY KEY designates the id field as the key field in the table. The key field should be
set to a field that cannot contain any duplicates.
The AUTO_INCREMENT command will automatically assign increasing values into the id field,
essentially automatically numbering each entry.
The CHAR(characters) and INT(integers) commands designate the types of data allowed in
those fields. The number next to the commands indicated how many characters or integers
can fit in the field.

Changing Table Definitions

The ALTER TABLE command can be used to add new columns to an existing table. Existing
columns may also be altered. Table constraints may be added or dropped. The ALTER TABLE
command may include keywords such as ADD, DROP, or ALTER and allow the columns names,
data type, length, and constraints to be changed. The ALTER command cannot be used to change
a view. Syntax:
ALTER TABLE table_name alter_table_action;
Some of the alter_table_actions available are:
ADD [COLUMN] column_definition ALTER [COLUMN] column_name SET DEFAULT default-value
ALTER [COLUMN] column_name DROP DEFAULT DROP [COLUMN] column_name [RESTRICT]
[CASCADE] ADD table_constraint
Command: To add a customer type column named CustomerType to the Customer table.
ALTER TABLE CUSTOMER_T ADD COLUMN CustomerType VARCHAR2 (2) DEFAULT Commercial;
The ALTER command is invaluable for adapting a database to inevitable modifi- cations due to
changing requirements, prototyping, evolutionary development, and mistakes. It is also useful
when performing a bulk data load into a table that contains a foreign key.
Removing Tables To remove a table from a database, the owner of the table may use the DROP
TABLE command. Views are dropped by using the similar DROPVIEW command. Command:
To drop a table from a database schema.
DROP TABLE Customer_T;
This command will drop the table and save any pending changes to the database. To drop a
table, you must either own the table or have been granted the DROP ANY TABLE system
privilege. Dropping a table will also cause associated indexes and privi- leges granted to be
dropped. The DROP TABLE command can be qualified by the keywords RESTRICT or CASCADE. If
RESTRICT is specified, the command will fail, and the table will not be dropped if there are any
dependent objects, such as views or constraints, that currently reference the table. If CASCADE is
specified, all dependent objects will also be dropped as the table is dropped. Many RDBMSs
allows users to retain the tables structure but remove all of the data that have been entered in
the table with its TRUNCATE TABLE command. Commands for updating and deleting part of the
data in a table are covered in the next section.

Creating and Managing Databases

Create the database.


From the MySQL command line, enter the command CREATE DATABASE <DATABASENAME>;
Replace <DATABASENAMEs> with the name of your database. It cannot include spaces.
For example, to create a database of all the US states, you might enter

CREATE DATABASE us_states;


Note: Commands do not have to be entered in upper-case.
Note: All MySQL commands must end with ";". If you forgot to include the semicolon, you
can enter just ";" on the next line to process the previous command.
Removing existing Database
Removing database means you delete the database physically. All the data and related objects inside the
database are permanently deleted and this cannot be undone, therefore it is very important to execute this
query with extra cautions.
To delete a database, you use the DROP DATABASE statement as follows:
DROP DATABASE [IF EXISTS] database_name;

Followed the DROP DATABASE is the database name that you want to remove. Similar to the CREATE
DATABASE statement, the IF EXISTS is an optional part of the statement to prevent you from removing a
database that does not exist in the database server.
If you want to practice with the DROP DATABASE statement, you can create a new database, make sure
that it is created and remove it. Take a look at the following queries:
CREATE DATABASE IF NOT EXISTS temp_database;
SHOW DATABASES;
DROP DATABASE IF EXISTS temp_database;

Display a list of your available databases.


Enter the command
SHOW DATABASES;
to list all of the databases you have stored. Besides the database you just
created, you will also see a mysql database and a test database.

You might also like