0% found this document useful (0 votes)
2 views15 pages

1 SQL DDL Commands

SQL, or Structured Query Language, is a non-procedural language used for querying databases and is the standard language for relational database management systems (RDBMS). It includes four categories of commands: DDL for defining database structures, DML for manipulating data, DCL for controlling access, and TCL for transaction control. The document also details SQL commands, data types, constraints, and examples of creating and modifying tables in MySQL.

Uploaded by

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

1 SQL DDL Commands

SQL, or Structured Query Language, is a non-procedural language used for querying databases and is the standard language for relational database management systems (RDBMS). It includes four categories of commands: DDL for defining database structures, DML for manipulating data, DCL for controlling access, and TCL for transaction control. The document also details SQL commands, data types, constraints, and examples of creating and modifying tables in MySQL.

Uploaded by

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

SQL - Structured Query Language

(Oracle, 1979)
SQL - Structured Query Language (Oracle, 1979)

• SQL is used to make a request to retrieve data from a Database.


• The DBMS processes the SQL request, retrieves the requested data from the Database, and
returns it.
• This process of requesting data from a Database and receiving back the results is called a
Database Query and hence the name Structured Query Language.
• SQL is a non-procedural language.
• Today, SQL is accepted as the standard RDBMS language.
• SQL is a language that all commercial RDBMS implementations understand.
DBMS SQL Commands – Four Categories

• DDL (Data Definition Language) – This category is used to define or modify or delete the
structure of database objects such as TABLE, VIEW, etc
– Create
– Alter
– Drop
– Truncate
• DML (Data Manipulation Language) – This category is used to manipulate the information
stored in database tables
– Insert
– Update
– Delete
– Select
• DCL (Data Control Language)
– Grant
– Revoke
• TCL (Transaction Control Language
– Commit
– Rollback
- Savepoint
DBMS (SQL) - Data Definition Language

Data Types supported by MySQL

• Numeric
• String
• Date and Time
DBMS (SQL) - Data Definition Language

MySQL – Constraints
• Constraints are used to specify the rule that permits or restricts
what values/data will be stored in the table or in a particular
column.
• Constraints guarantee data accuracy and integrity inside the table.
• If any mismatch occurs between the constraint and data action,
the action is failed.
DBMS (SQL) - Data Definition Language

MySQL – Constraints

• Column Level Constraints Purpose


• Table Level NOT NULL
To ensure that a column does not accept NULL
values.
To ensure that a column accepts values within the
CHECK
specified range of values.
It ensures that a column does not accept duplicate
UNIQUE
values.
Uniquely identifies a record in the table.
PRIMARY KEY
Combination of NOT NULL and UNIQUE.

FOREIGN KEY Is used to set relationship between two tables

It ensures that the column sets a default value for


DEFAULT
empty records.

Auto_Increment, Index, Enum


DBMS (SQL) - Data Definition Language

• Create (Database or Table)


• Alter
– Add column or constraints
– Drop column or constraints
– Modify or change column name or data type
– Modify or change constraints
– Rename Table
• Truncate
• Drop
Stud_ID Stud_NAME Stud_AGE Stud_rollno
DBMS (SQL) - Data Definition Language

SQL CREATE Command

SQL Syntax – General Form

SQL Syntax – An example


DBMS (SQL) - Data Definition Language

Create Table with Not NULL, Primary Key and other constraints

• Creating table with primary key : Customer_info Table:

CREATE TABLE Customer_info


( Cust_ID int(2) PRIMARY KEY,
Cusr_Name varchar(20),
Cust_Email varcahr(35),
Mobno varchar(10) );

• Composite Key : Two or columns grouped together to form a primary key


• CREATE TABLE Customer_Details
(Cust_ID Number(5) NOT NULL,
Cust_Name VarChar(20) NOT NULL, Account_No int(5) NOT NULL,
Account_Type VarChar(10) NOT NULL,
Bank_Branch VarChar(25) NOT NULL,
Cust_Email VarChar2(30), PRIMARY KEY (Cust_ID, Account_No)
);

• Adding Primary key to the table after its creation:


ALTER TABLE Customer_info ADD PRIMARY KEY (Cust_ID);
DBMS (SQL) - Data Definition Language
MySQL – Constraints

Create Table with different Constraints (More examples)


Example 1:
mysql> CREATE TABLE Persons ( Example 4:
ID int NOT NULL, mysql> CREATE TABLE ShirtBrands(
Name varchar(45) NOT NULL, Id INTEGER,
Age int CHECK (Age>=18 AND Age <=60) BrandName VARCHAR(40) UNIQUE,
); Size VARCHAR(30));

Example 2: Example 5:
mysql> CREATE TABLE Persons ( mysql> CREATE TABLE Animals(
ID int NOT NULL, id int NOT NULL AUTO_INCREMENT,
Name varchar(45) NOT NULL, name VARCHAR(30) NOT NULL,
Age int, PRIMARY KEY (id));
City varchar(25) DEFAULT 'New York'
); Example 6:
mysql> CREATE TABLE Shirts (
Example 3: id INT PRIMARY KEY AUTO_INCREMENT,
CREATE TABLE Persons ( name VARCHAR(35),
ID int NOT NULL PRIMARY KEY, size ENUM('small', 'medium', 'large', 'x-large')
Name varchar(45) NOT NULL, );
Age int,
City varchar(25));
DBMS (SQL) - Data Definition Language

SQL ALTER Command - Modify the structure of the table

ALTER - SQL Syntax – General Form


DBMS (SQL) - Data Definition Language

ALTER - Modify the structure of the table

• Add/Drop Column - Syntax: ALTER TABLE tablename (ADD/MODIFY/DROP column_name)

ALTER - SQL Syntax – Some examples

ALTER TABLE Customer_Details MODIFY Contact_Phone varchar(12); Size changed


ALTER TABLE Customer_Details ADD Contact_Phone Char(10); New column added
ALTER TABLE Customer_Details DROP (Contact_Phone); Column deleted

• Add/Drop Primary Key Constraint after the Table has been created

ALTER TABLE Customer_Details ADD CONSTRAINT Pkey1 PRIMARY KEY (Account_No);


ALTER TABLE Customer_Details ADD CONSTRAINT Pkey2 PRIMARY KEY (Account_No, Cust_ID);
ALTER TABLE Customer_Details DROP PRIMARY KEY;
DBMS (SQL) - Data Definition Language
Truncate, Drop and Delete

SQL Syntax :
• Truncate Tablename
• Truncate Student - Will delete all records but the structure will remain intact
• Drop Table Tablename
• Drop Table Student - Will delete whole table (structure as well as content).

For selective Deletion (DML)


•Delete from Student Where Stud_ID= ‘101’- For Selective deletion
• Delete from Student - Will delete all records but the structure will remain intact
MySQL – Operators to Manipulate Table Content
(Used with DDL and DML Commands)

1. Arithmetic 2. Relational 3. Logical 4. Other MySQL operators

LIKE, BETWEEN, IN, NOT NULL


MySQL – Operators to Manipulate Table Content
(Used with DDL and DML Commands)

1. Arithmetic 2. Relational 3. Logical 4. Othe MySQL operators

Relational Operators Logical and other MySQL Operator


Operator Description
Operator Description
If the value of left operand is greater than that of the value of the
> BETWEE It is used to search within a set of values, by the minimum value and
right operand, the condition becomes true; if not then false.
N maximum value provided.
If the value of left operand is less than that of a value of the right It is used to search for the presence of a row in a table which satisfies a
< EXISTS
operand, the condition becomes true; if not then false. certain condition specified in the query.
If both the operands have equal value, the condition becomes true; if It is used to combine multiple conditions in a statement by using the
= OR
not then false. WHERE clause.
If both the operands do not have equal value, the condition becomes It allows the existence of multiple conditions in an SQL statement’s
!= AND
true; if not then false. WHERE clause.
It reverses the meaning of the logical operator with which it is used.
If the value of left operand is greater than or equal to the right NOT
>= (Examples: NOT EXISTS, NOT BETWEEN, NOT IN, etc.)
operand, the condition becomes true; if not then false.
IN It is used to compare a value in a list of literal values.
If the value of left operand is less than or equal to the right operand, ALL It compares a value to all values in another set of values.
<=
the condition becomes true; if not then false. It compares a value to any value in the list according to the condition
ANY
specified.
If the value of left operand is not less than the value of right operand,
!<
the condition becomes true; if not then false. LIKE It uses wildcard operators to compare a value to similar values.

If the value of left operand is not greater than the value of right IS NULL It compares a value with a NULL value.
!>
operand, the condition becomes true; if not then false.
It searches for every row of a specified table for uniqueness (no
If the values of two operands are not equal, the condition becomes UNIQUE
<> duplicates).
true; if not then false.

You might also like