0% found this document useful (0 votes)
3 views

DDL DML and Constraints

The document provides an overview of MySQL, focusing on its data types, database management systems, and the distinction between Data Definition Language (DDL) and Data Manipulation Language (DML). It includes examples of various DDL commands such as creating, altering, and dropping tables, as well as DML commands for selecting, inserting, updating, and deleting records. Additionally, it discusses constraints in MySQL, including NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, FOREIGN KEY, and CHECK constraints.

Uploaded by

Riddhish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DDL DML and Constraints

The document provides an overview of MySQL, focusing on its data types, database management systems, and the distinction between Data Definition Language (DDL) and Data Manipulation Language (DML). It includes examples of various DDL commands such as creating, altering, and dropping tables, as well as DML commands for selecting, inserting, updating, and deleting records. Additionally, it discusses constraints in MySQL, including NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, FOREIGN KEY, and CHECK constraints.

Uploaded by

Riddhish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

MySQL

DDL and DML


Introduction
 Database
 Collection of Interrelated Data
 Database Management Systems
 Set of programs used to manipulate database
 Relational Database
 A database structured to recognize relations between stored
items of information.
 Table – Real World Object
 Attribute / Field – Properties if a Table
 Tuple / Record – an ordered set of data constituting a
record
 Domain / Column – Set of all values of an attribute
Introduction
MySQL Data types
 String types
 CHAR(n) – fixed-length character data, n characters long Maximum length = 2000

bytes

 VARCHAR2(n) – variable length character data, maximum 4000 bytes

 LONG – variable-length character data, up to 4GB. Maximum 1 per table

 Numeric types
 NUMBER(p,q) – general purpose numeric data type

 INTEGER(p) – signed integer, p digits wide

 FLOAT(p) – floating point in scientific notation with p binary digits precision

 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)

 Data Definition Language (DDL)


 Commands that define a database, including creating, altering, and dropping tables and

establishing constraints

 Data Manipulation Language (DML)


 Commands that maintain and query a database

 Data Control Language (DCL)


 Commands that control a database, including administering privileges and committing

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

create table student(name varchar2(15), rollno number(8), dept


varchar2(5), doj date);

 Describe Table

MySQL> desc student


Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(15)
ROLLNO NUMBER(8)
DEPT VARCHAR2(5)
DOJ DATE
DDL - EXAMPLE
 Alter Table

 Add

MySQL> alter table student add regno number(15);

Table altered.

MySQL> desc student


Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(15)
ROLLNO NUMBER(8)
DEPT VARCHAR2(5)
DOJ DATE
REGNO NUMBER(15)
DDL - EXAMPLE
 Alter Table

 Modify – Data Type and Attribute Size

 Modify – Data Type

MySQL> alter table student modify rollno varchar2(7);

Table altered.

MySQL> desc student


Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(15)
ROLLNO VARCHAR2(7)
DEPT VARCHAR2(5)
DOJ DATE
REGNO NUMBER(15)
DDL - EXAMPLE
 Alter Table

 Modify – Attribute Size

MySQL> alter table student modify rollno varchar2(8);

Table altered.

MySQL> desc student


Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(15)
ROLLNO VARCHAR2(8)
DEPT VARCHAR2(5)
DOJ DATE
REGNO NUMBER(15)
EXAMPLE – Insert
Command
MySQL> insert into student values('kalai','vt1001','cse','01-aug-
2010',4001);

1 row created.

MySQL> insert into student values('Pon','vt1002','cse','01-jul-


2010',4002);

1 row created.

MySQL> select * from student;

NAME ROLLNO DEPT DOJ REGNO


--------------- -------- ----- --------- ----------
kalai vt1001 cse 01-AUG-10 4001
Pon vt1002 cse 01-JUL-10 4002
DDL - EXAMPLE
 Alter Table

 Modify – Drop

MySQL> alter table student drop column regno number(15);

Table altered.

MySQL> desc student


Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(15)
ROLLNO VARCHAR2(8)
DEPT VARCHAR2(5)
DOJ DATE
DDL - EXAMPLE
 Truncate Table

MySQL> truncate table student;

Table truncated.

MySQL> select * from student;

no rows selected

MySQL> desc student


Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(15)
ROLLNO VARCHAR2(8)
DEPT VARCHAR2(5)
DOJ DATE
REGNO NUMBER(15)
DDL - EXAMPLE
 Drop Table

MySQL> drop table student;

Table dropped.

MySQL> select * from student;


select * from student
*
ERROR at line 1:
ORA-00942: table or view does not exist

MySQL> desc student


ERROR:
ORA-04043: object student does not exist
DML - Explanation
MySQL> create table player(name varchar2(20), country
varchar2(15), matches number(3), runs number(4), jersy
number(2));

Table created.

Insert Command

Method 1 - Inserting Single records

MySQL> insert into player values('sachin','india',98,999,9);

1 row created.
DML - Explanation
Method 2 - Inserting Multiple records

MySQL> insert into player


values('&name','&country',&matches,&runs,&jersy);
Enter value for name: shewag
Enter value for country: india
Enter value for matches: 15
Enter value for runs: 750
Enter value for jersy: 5
old 1: insert into player
values('&name','&country',&matches,&runs,&jersy)
new 1: insert into player values('shewag','india',15,750,5)

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

MySQL> insert into player(name,country,matches)


values('pathani','india',02);

1 row created.
DML - Explanation
Select Command

Selecting the overall contents (tuples) of the relation

MySQL> select * from player;

NAME COUNTRY MATCHES RUNS JERSY


-------------------- --------------- ---------- ---------- ----------
sachin india 98 999 9
shewag india 15 750 5
gilchrist australia 50 5000 3
pathani india 2

4 rows selected.
DML - Explanation
Selecting domain values

MySQL> select name,country from player;

NAME COUNTRY
-------------------- ---------------
sachin india
shewag india
gilchrist australia
pathani india

4 rows selected.
DML - Explanation
Selecting values by avoiding repetitions and duplications

MySQL> select distinct country from player;

COUNTRY
---------------
australia
india
DML - Explanation
Selecting tuple values based on conditions

MySQL> select * from player where matches>=40;

NAME COUNTRY MATCHES RUNS JERSY


-------------------- --------------- ---------- ---------- ----------
sachin india 98 999 9
gilchrist australia 50 5000 3
DML - Explanation
Selecting domain values based on conditions

MySQL> select name,runs from player where runs>=1000;

NAME RUNS
-------------------- ----------
gilchrist 5000
DML - Explanation
 Selecting tuple values based on sorting in ascending order

MySQL> select * from player order by matches;

 Selecting tuple values based on sorting in ascending order by


specifying asc

MySQL> select * from player order by matches asc;

 Selecting tuple values based on sorting in descending order by


specifying desc

MySQL> select * from player order by matches desc;

 Selecting tuple values based on sorting with both asc and desc

MySQL> Select * from player order by matches asc, country desc;


DML - Explanation
 Selecting tuple values by using string constants

 Selecting tuple values whose name starts with ‘s’

MySQL> select * from player where name like 's%';

 Selecting tuple values whose 5 character name starts with‘d’ and


ends with ‘i’

MySQL> select * from player where name like 'd___i';

 Selecting tuple values whose name has character ‘c’

MySQL> select * from player where name like '%c%';

 Renaming domain name

MySQL> select name player,country nation from player;


DML - Explanation
 Update Command

 Change the runs as 10 whose jersy number is 10

MySQL> update player set runs=10 where jersy=10;

 Change the runs as 10 whose jersy number is 1null

MySQL> update player set runs=10 where jersy is null;

3 rows updated.
DML - Explanation
 Delete Command

 Deleting based on conditions

MySQL> delete from player where jersy is null;

 Deleting overall contents from the relation

MySQL> delete from player;

MySQL> select * from player;

no rows selected
Rename Table / Rename Attribute /
Copy a Table
 copy a table
create table table_name as select * from table_name;

 Rename a column name


alter table table_name rename column old_name to new_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.

MySQL> desc acct

Name Null? Type


----------------------------------------- -------- ---------
ACCTNO NOT NULL NUMBER(5)
BALANCE NOT NULL NUMBER(12,2)

MySQL> insert into acct values(1,1999.50);

1 row created.
NOT NULL CONSTRAINT
SHOWS ERROR WHEN NULL VALUE IS INSERTED FOR BALANCE ATTRIBUTE

MySQL> insert into acct values(null,1999.50);


insert into acct values(null,1999.50)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("JUJO"."ACCT"."ACCTNO")

SHOWS ERROR WHEN NULL VALUE IS INSERTED FOR ACCTNO ATTRIBUTE

MySQL> insert into acct values(1,null);


insert into acct values(1,null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("JUJO"."ACCT"."BALANCE")
NOT NULL CONSTRAINT
 Adding a Not Null constraint to
existing table:
ALTER TABLE table_name MODIFY
column_name datatype NOT NULL;
UNIQUE CONSTRAINT
MySQL> create table studs1(regno number(12),name varchar2(10),city
varchar2(7), constraint mynuique unique(name));

Table created.

MySQL> desc studs


Name Null? Type
----------------------------------------- -------- -------------
REGNO NUMBER(12)
NAME VARCHAR2(10)
CITY VARCHAR2(7)
UNIQUE CONSTRAINT
MySQL> insert into studs values(1001,'geetha','karai');

1 row created.

MySQL> insert into studs values(1002,'seetha','erode');

1 row created.

SHOWS ERROR WHEN NON UNIQUE VALUE IS INSERTED FOR NAME ATTRIBUTE

MySQL> insert into studs values(1003,'geetha','salem');


insert into studs values(1003,'geetha','salem')
*
ERROR at line 1:
ORA-00001: unique constraint (JUJO.SYS_C003208) violated
UNIQUE CONSTRAINT
 Adding Unique constraint to existing table:
ALTER TABLE table_name ADD CONSTRAINT
MyUniqueConstraint UNIQUE(column1, column2...);

 Drop Unique constraint from the table:


ALTER TABLE table_name DROP CONSTRAINT
MyUniqueConstraint;
DEFAULT CONSTRAINT
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2) DEFAULT 5000.00,
PRIMARY KEY (ID)
);
ADD DEFAULT
CONSTRAINT
 If CUSTOMERS table has already been
created, then to add a DFAULT constraint
to SALARY column, you would
 write a statement similar to the
following

ALTER TABLE CUSTOMERS


MODIFY SALARY DECIMAL (18, 2) DEFAULT
5000.00;
CHECK CONSTRAINT
MySQL> create table stu(name varchar2(10) not null, studid
varchar2(5), degreelevel varchar2(15), primary key(studid),
check(degreelevel in('Bachelors','Masters','Doctorate')));

Table created.
MySQL> desc stu

Name Null? Type


----------------------------------------- -------- -------------
NAME NOT NULL VARCHAR2(10)
STUDID NOT NULL VARCHAR2(5)
DEGREELEVEL VARCHAR2(15)
CHECK CONSTRAINT
MySQL> insert into stu values('hannah',101,'Masters');

1 row created.

MySQL> insert into stu values('kumar',102,'Bachelors');

1 row created.

MySQL> insert into stu values('karthick',103,'Doctorate');

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.

MySQL> select * from stu;

NAME STUDI DEGREELEVEL


---------- ----- ---------------
hannah 101 Masters
kumar 102 Bachelors
karthick 103 Doctorate
jeni 104 Bachelors
Create table with Primary
Key
 Primary Key:
 Used to identify a record uniquely.
 It is applied to any attribute
 Value of a primary key should not be null and should not
be repeated again in the domain.

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;

Note: pk is the user defined name


given to primary key constraint.
Foreign Key – customer
table
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
Foreign Key – orders table
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references
CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
Common syntax for adding a
constraint to the existing table
MySQL> alter table table_name add
constraint constraint_name
constraint_type(attribute_name);
Common syntax for dropping a
constraint from a table
MySQL> drop constraint
constraint_name;
Thank you!

You might also like