DDL DML and Constraints
DDL DML and Constraints
bytes
Numeric types
NUMBER(p,q) – general purpose numeric data type
Date/time type
DATE – fixed-length date/time in dd-mm-yy form
MySQL Environment
Catalog
A set of schemas that constitute the description of a database
Schema
The structure that contains descriptions of objects created by a user (base tables,
views, constraints)
establishing constraints
data
MySQL: DDL Commands
Create table
Describe
Alter table
Add
Attribute
Constraints
Rename
Table
Attribute name
MySQL: DDL Commands
Modify
Data type
Size of attribute
Constraints
Drop
Attribute
Constraints
Truncate table
Drop table
DDL - Explanation
Create Table – Creates a relation with its properties
named attributes
Describe Table – Views the structure of the relation with
the data type of each attributes with their maximum
memory size it can hold
Alter Table – Alters the structure of the relation
Add – Adds a new attribute
Modify – Changes the data type and size of an attribute
Drop – Deletes an attribute
Truncate Table – Deletes the over all contents of the
relation. Structure remains
Drop Table - Deletes the over all contents of the
relation along with the structure.
MySQL: DML Commands
Select
Insert
Update
Delete
DML - Explanation
Insert Command – Inserts values into the relation.
Non values should be entered as Null.
Method 1 - Inserting Single records
Method 2 - Inserting Multiple records
Method 3 - Inserting Values for Specific attributes
Select Command – Selects tuple, domain values and
various conditions can be applied during selection
Update Command – Modifies the content of the
relation
Delete Command – Deletes the contents of the
relation and conditions can be applied during
deletion.
DDL - EXAMPLE
Create table
Describe Table
Add
Table altered.
Table altered.
Table altered.
1 row created.
1 row created.
Modify – Drop
Table altered.
Table truncated.
no rows selected
Table dropped.
Table created.
Insert Command
1 row created.
DML - Explanation
Method 2 - Inserting Multiple records
1 row created.
DML - Explanation
MySQL> /
Enter value for name: gilchrist
Enter value for country: australia
Enter value for matches: 50
Enter value for runs: 5000
Enter value for jersy: 3
old 1: insert into player
values('&name','&country',&matches,&runs,&jersy)
new 1: insert into player
values('gilchrist','australia',50,5000,3)
1 row created.
DML - Explanation
Method 3 - Inserting Values for selected
attributes
1 row created.
DML - Explanation
Select Command
4 rows selected.
DML - Explanation
Selecting domain values
NAME COUNTRY
-------------------- ---------------
sachin india
shewag india
gilchrist australia
pathani india
4 rows selected.
DML - Explanation
Selecting values by avoiding repetitions and duplications
COUNTRY
---------------
australia
india
DML - Explanation
Selecting tuple values based on conditions
NAME RUNS
-------------------- ----------
gilchrist 5000
DML - Explanation
Selecting tuple values based on sorting in ascending order
Selecting tuple values based on sorting with both asc and desc
3 rows updated.
DML - Explanation
Delete Command
no rows selected
Rename Table / Rename Attribute /
Copy a Table
copy a table
create table table_name as select * from table_name;
Rename a table
alter table table_name rename to new_name;
Examples:
alter table student rename column branch to course;
alter table student rename to students;
Rename Table / Rename Attribute /
Copy a Table
Grant Command
a. Granting privileges for a relation of
one user to be accessed from another
user.
b. Granting privilege privileges for a
user to grant privilege for another user.
Revoke Command
Getting back the privileges given
from a user.
MySQL CONSTRAINTS
NOT NULL Constraint: Ensures that a column cannot have
NULL value.
DEFAULT Constraint: Provides a default value for a column
when none is specified.
UNIQUE Constraint: Ensures that all values in a column are
different.
PRIMARY Key: Uniquely identified each rows/records in a
database table.
FOREIGN Key: Uniquely identified a rows/records in any
another database table.
CHECK Constraint: The CHECK constraint ensures that all
values in a column satisfy certain conditions.
INDEX : Use to create and retrieve data from the database
very quickly.
NOT NULL CONSTRAINT
MySQL> create table acct(acctno number(5) not null, balance
number(12,2) not null);
Table created.
1 row created.
NOT NULL CONSTRAINT
SHOWS ERROR WHEN NULL VALUE IS INSERTED FOR BALANCE ATTRIBUTE
Table created.
1 row created.
1 row created.
SHOWS ERROR WHEN NON UNIQUE VALUE IS INSERTED FOR NAME ATTRIBUTE
Table created.
MySQL> desc stu
1 row created.
1 row created.
1 row created.
CHECK CONSTRAINT
SHOWS ERROR WHEN WRONG VALUE IS INSERTED FOR DEGREELEVEL
ATTRIBUTE
MySQL> insert into stu values('jensi',104,'Mastres');
insert into stu values('jensi',104,'Mastres')
*
ERROR at line 1:
ORA-02290: check constraint (JUJO.SYS_C003210) violated
CHECK CONSTRAINT
MySQL> insert into stu values('jeni',104,'Bachelors');
1 row created.
Syntax:
create table table_name(attribute_name1 data_type
primary key, attribute_name2 data_type);
Or
create table table_name(attribute_name1 data_type,
attribute_name2 data_type, primary
key(attribute_name1));
Example
create table student(regno number(5)
primary key, name varchar2(15), rollno
number(8), dept varchar2(5), doj date);
Or
create table student(regno number(5), name
varchar2(15), rollno number(8), dept
varchar2(5), doj date, primary
key(regno));
Add primary key
MySQL> alter table table_name add
constraint pk primary
key(attribute_name);
Drop primary key
MySQL> alter table table_name drop
primary key;
Or
MySQL> alter table table_name drop
pk;