CHAPTER-13
TABLE CREATION AND
DATA MANIPULATION
CREATING DATABASES
Creating a DATABASE is an easier
task relatively.
CREATE DATABASE myDb;
CREATE DATABASE IF NOT EXISTS myDb;
USE myDb;
SHOW DATABASES;
DROP DATABASE myDb;
CREATING TABLES
When a table is created, its columns are named,
datatypes and sizes are supplied for each column.
CREATE TABLE employee
(ecode integer,
ename char(20),
sex char(1),
grade char(2),
gross decimal);
DATA INTEGRITY THROUGH CONSTRAINTS
A Constraint is a condition or check applicable on a field or
set of fields. The constraint applied to maintain data integrity
are also known as integrity constraints.
The two basic type of constraints are column constraints
table constraints.
Column Constraint :- Apply only to individual columns
Table Constraint:- Apply to groups of one or more columns
CREATE TABLE employees
( ecode integer NOT NULL,
ename char(20) NOT NULL,
sex char(1) NOT NULL,
grade char(2),
gross decimal);
DIFFERENT CONSTRAINTS
Unique Constraint
Primary Key Constraint
Default Constraint
Check Constraint
Foreign Key Constraint
DIFFERENT CONSTRAINTS
Unique Constraint
This specify no two rows have the same
value in the specified columns
Primary Key Constraint CREATE TABLE employess
(
ecode integer NOT NULL UNIQUE,
ename char(20) NOT NULL,
Default Constraint sex char(1) NOT NULL,
grade char(2),
gross decimal
);
Check Constraint
Foreign Key Constraint
DIFFERENT CONSTRAINTS
Unique Constraint
This declares a column as the primary key
of the table. This constraint is similar to
unique constraint except that only one
column can be applied in this constraint .
Primary Key Constraint
The primary key cannot allow NULL values
CREATE TABLE employes
Default Constraint (
ecode integer NOT NULL PRIMARY KEY,
ename char(20) NOT NULL,
sex char(1) NOT NULL,
Check Constraint grade char(2),
gross decimal
);
Foreign Key Constraint
DIFFERENT CONSTRAINTS
Unique Constraint
When a user does not enter a value for the
column, automatically the defined default
value is inserted in the field.
Primary Key Constraint
CREATE TABLE employes
(
Default Constraint ecode integer NOT NULL PRIMARY KEY,
ename char(20) NOT NULL,
sex char(1) NOT NULL,
grade char(2) DEFAULT ‘E1’,
Check Constraint gross decimal
);
Foreign Key Constraint
DIFFERENT CONSTRAINTS
This limits the values that can be inserted
into a column of a table.
Unique Constraint
CREATE TABLE employes
(
ecode integer NOT NULL PRIMARY KEY,
Primary Key Constraint ename char(20) NOT NULL,
sex char(1) NOT NULL,
grade char(2) DEFAULT ‘E1’,
gross decimal CHECK(gross>2000)
Default Constraint );
CREATE TABLE items
( icode char(5) NOT NULL PRIMARY KEY,
descp char(20) NOT NULL,
Check Constraint rol integer,
qoh integer,
CHECK (rol<qoh));
Foreign Key Constraint
DIFFERENT CONSTRAINTS
1).
Unique Constraint
Descp char(20) CHECK
(descp
IN(‘NUT’,’BOLT’,’SCREW’,’WRENCH’,’NAIL’))
2)
Primary Key Constraint Price decimal CHECK(price BETWEEN 253.00
and 770.00)
3)Ordate char(10) NOT NULL CHECK(ordate
LIKE ‘- -/- -/- - - -’)
Default Constraint
4) CHECK (
( discount=0.15 AND city=‘HISSAR’)OR
( discount=0.13 AND city=‘JAIPUR’)OR
Check Constraint ( discount=0.17 AND city=‘MOHALI’))
Foreign Key Constraint
DIFFERENT CONSTRAINTS
Whenever two tables are related by a common
Unique Constraint column, then the columns in the parent table
should be a primary key or unique key and
the related columns in the child table should
have foreign key constraint
Primary Key Constraint CREATE TABLE Items
(
itemno char(5) NOT NULL PRIMARY KEY);
CREATE TABLE orders
Default Constraint ( orderno number(6,0) NOT NULL PRIMARY
KEY,
Itemno char(5) REFERENCES item(itemno)
);
Check Constraint
Foreign Key Constraint
DIFFERENT CONSTRAINTS
Unique Constraint 1)If you skip the related columnname in
parent table then by default MYSQL will
reference the primary key of parent table
2)If you write ON DELETE CASCADE also, while
Primary Key Constraint defining foreign key constraint then in case a
row or a tuple is deleted in parent table, all its
related tuples or rows in the child table will
automatically be deleted.
Default Constraint 3)If you write ON UPDATE CASCADE also, a
row or tuple is updated in parent table, all its
related tuples or rows in the child table will
automatically be updated.
Check Constraint
Foreign Key Constraint
APPLYING TABLE CONSTRAINTS
When a constraint is to be applied on a group of
columns of the table it is called table constraint.
It appears in the end of table definition.
CREATE TABLE items
(icode char(5) NOT NULL,
descp char(20) NOT NULL, CREATE TABLE members
ROL integer, (firstname char(15) NOT NULL,
QAH integer, lastname char(15) NOT NULL,
CHECK (ROL<QOH), City char(20),
UNIQUE(icode,descp) PRIMARY KEY(firstname,lastname)
); );
Create table orders
(
Orderno number(6,0) NOT NULL,
Itemno char(5),
FOREIGN KEY (orderno,Itemno)REFERENCES
(OrderNO,ItemNO) );
QUESTION 1
Create a table ID Number(4),Firstname varchar(30),Lastname varchar(30) user_id
varchar(10),salary number(32)
1) id should be declared as primary key
2) user_id should be unique
3) Salary must be greater than 5000
4) first_name and last_name must not remain blank
QUESTION 2
Create a table JOB_ID Number(4),JOB_DES varchar(30),ALLOC_ON Date,DUE_ON
Date,Emp_id number(4)
1) Job_id should be declared as primary key
2) JOB_DES,ALLOC_ON,DUE_ON should be unique
3) EMP_ID is foreign key of ID in above table.
VIEWING OF A TABLE STRUCTURE
DESC[RIBE] <Tablename>;
SHOW TABLES;
CREATING TABLE FROM EXISTING TABLE
Show columns from<tablename>;
CHANGING DATA WITH DML COMMANDS
INSERT INTO COMMAND
BOOK PAGE NO.577 Q13.3
INSERT INTO NULL VALUE
CHANGING DATA WITH DML COMMANDS
INSERT DATES
INSERT DATA FROM ANOTHER INSERT INTO BRANCH1
DATA SELECT * FROM BRANCH2
WHERE GROSS>7000.00
BOOK PAGE NO.578 Q13.4
MODIFYING DATA WITH UPDATE COMMAND
UPDATE MULTIPLE COLUMNS
USING EXPRSSION ON UPDATE
UPDATING TO NULL VALUES
DELETING DATA WITH DELETE COMMAND
Delete command removes rows from a table. It remove entire rows ,
not individual field values.
ALTER TABLE COMMAND
Alter command is used to change definition of existing tables. It adds
column or sometimes delete columns or change the sizes
To add a column To add an integrity constraints
To redefine a column( Datatype, size,
default values)
ADDING COLUMNS
Table Emp add a column THRIFTPLAN number(7) and 2 decimal
place(7,2) ,LOANCODE char(1) and an integrity that checks that load code
should be one of these characters:’E’,’C’,’H’,’P’
Table Emp add a column special pay number(7) and 2 decimal
place(7,2) ,and with a default value of 2400.
MODIFYING COLUMN DEFINATIONS
You can change a char column to varchar and vice versa only if the
column contains nulls in all rows or if you do not attempt to change
Datatypes and size
the column size.
A change of default value only affects rows subsequently inserted into
Default values the table. Such a change doesn’t previously inserted.
For NOT NULL we can add constraint by using MODIFY clause.
Unique,primary key ,referential and check constraint can be added to
Integrity constraint
existing columns by use ADD clause.
Order of column With MODIFY clause can reorder column within table. Use FIRST or
AFTER clause to specify the position of the column.
MODIFYING COLUMN DEFINATIONS
Order of column
Practical question pg:- 582
CHANGING COLUMN NAME
REMOVING TABLE COMPONENTS
If planning to remove components of table use DROP
clause of ALTER Table.
Primary key :- Drop the table’s primary key constraint
Column name:- Remove mention column of the table
Foreign key :- Remove the mentioned foreign key
constraint from the table.
Alter table emp Practical Question
Drop primary key ,drop foreign key fk_1,drop column page 583
deptno;
DROP TABLE COMMAND
Drop a table from the database.