0% found this document useful (0 votes)
68 views8 pages

SQL DDL Commands and Table Creation

The document discusses the different components of SQL including Data Definition Language (DDL), Data Manipulation Language (DML), Data Retrieval Language (DRL), Transaction Control Language (TCL), and Data Control Language (DCL). It provides examples of using DDL statements like CREATE, ALTER, DROP, and TRUNCATE to create and modify database tables. It also outlines exercises to create tables for clients, products, and salesmen using DDL commands and insert sample data.

Uploaded by

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

SQL DDL Commands and Table Creation

The document discusses the different components of SQL including Data Definition Language (DDL), Data Manipulation Language (DML), Data Retrieval Language (DRL), Transaction Control Language (TCL), and Data Control Language (DCL). It provides examples of using DDL statements like CREATE, ALTER, DROP, and TRUNCATE to create and modify database tables. It also outlines exercises to create tables for clients, products, and salesmen using DDL commands and insert sample data.

Uploaded by

Dr.U Seshadri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

INTRODUCTION

SQL: It is structured query language, basically used to pass the query to retrieve and manipulate the
information from database. Depending upon the nature of query, SQL is divided into different
components:

SQL is divided into the following

  Data Definition Language (DDL)

 Data Manipulation Language (DML)

 Data Retrieval Language (DRL)

 Transaction Control Language (TCL)

 Data Control Language (DCL)

DDL -- create, alter, drop, truncate, rename

DML -- insert, update, delete

DRL -- select

TCL -- commit, rollback, savepoint

DCL -- grant, revoke

DDL: The Data Definition Language (DDL) is used to create the database (i.e. tables, keys, relationships
etc), maintain the structure of the database and destroy databases and database objects.

Eg. Create, Drop, Alter, Rename, Truncate

i. CREATE TABLE : It is used to create the table.


SYNTAX
Create table <table_name> (col1 datatype1(size),
col2 datatype2(size),

coln datatypen(size));
Ex: SQL> create table student (no number (2) ,
name varchar (10) ,
marks number (3));
ii. DROP statements: To destroy an existing database, table, index, or view. If a table is dropped all
records held within it are lost and cannot be recovered.

Syntax: DROP TABLE table_name;

1
iii. ALTER statements: To modify the structure of an existing database object.

a. Adding new columns:

Syntax:

Alter table table_name Add(New_columnName1 datatype(size), New_columnName2


datatype(size), ………);

b. Dropping a columns from a table :

Syntax:

Alter table table_name DROP column columnName

c. Modifying Existing columns:

Syntax:

Alter table table_name Modify (columnName1 New datatype(Newsize));

USING ALTER

  This can be used to add or remove columns and to modify the precision of the datatype.

  a) ADDING COLUMN

  Syntax: alter table <table_name> add <col datatype>;

  Ex: SQL> alter table student add sdob date;

  b) REMOVING COLUMN

  Syntax: alter table <table_name> drop <col datatype>;

  Ex: SQL> alter table student drop column sdob;

c) INCREASING OR DECREASING PRECISION OF A COLUMN

  Syntax: alter table <table_name> modify <col datatype>;

Ex: SQL> alter table student modify marks number(5);

  *Note: To decrease precision the column should be empty.

d) RENAMING COLUMN

  Syntax: alter table <table_name> rename column <old_col_name> to <new_col_name>;

  Ex: SQL> alter table student rename column marks to smarks;

2
[Link] statements: To destroy the data in an existing database, table, index, or view. If a table is
truncated all records held within it are lost and cannot be recovered but the table structure is
maintained.

Syntax : TRUNCATE TABLE table_name;

v. USING RENAME

  This will be used to rename the database object;

  Syntax: rename <old_table_name> to <new_table_name>;

  Ex: SQL> rename student to stud;

***

3
Week 1- Lab Exercises

Aim : Creation of Tables: Client_master, Product_master, Sales_master using DDL commands

Procedure:

Step 1: Creation of Table


CREATE TABLE CLIENT_MASTER(
CLIENT_NO CHAR(6) PRIMARY KEY,
NAME VARCHAR2(20) NOT NULL,
ADDRESS VARCHAR2(100),
CITY VARCHAR2(15),
PINCODE NUMBER(6),
STATE VARCHAR2(20),
BAL_DUE(8,2));
Step 2: Inserting records into the Table CLIENT_MASTER table:

INSERT INTO Client_Master(ClientNo,Name,City,PinCode,State,BalDue)


VALUES('C00001','Ivan Bayross','Mumbai',400054,'Maharashtra',15000);

INSERT INTO Client_Master(ClientNo,Name,City,PinCode,State,BalDue)


VALUES('C00002','Mamta Muzumdar','Madras",780001,'Tamil Nadu',0);

Modifying the Table definition:


To change the format of an existing table , we can use ALTER command

4
Sample queries
Q1: Retrieve the names, city and state of all the clients.

Solution: SELECT NAME, CITY, STATE FROM CLIENT_MASTER;

Q2: Print the structure of Client_Master table.

Solution: DESC CLIENT_MASTER;

Q3: List the names of all clients having ‘a’ as the second letter in their names.

Solution: SELECT NAME FROM CLIENT_MASTER WHERE NAME LIKE ‘_a%’;

Q4: List the names of all clients having ‘a’ as the second letter in their city name.

Solution: SELECT NAME FROM CLIENT_MASTER WHERE CITY LIKE ‘_a%’;

Q5: List all the clients who stay in ‘Bombay’ or ‘Delhi’.

Solution: SELECT NAME FROM CLIENT_MASTER WHERE CITY = ‘Bombay’ OR ‘Delhi’;

Q6: List all the clients who are located in Madras.

Solution:SELECT NAME FROM CLIENT_MASTER WHERE CITY = ‘Madras’;

Q7: List all clients whose Bal_Due is greater than the value 1000.
5
Solution: SELECT NAME FROM CLIENT_MASTER WHERE BAL_DUE > 1000;

Q8: List the names, city and state of clients who are not in the state of ‘Maharashtra’.

Solution: SELECT NAME, CITY, STATE FROM CLIENT_MASTER WHERE NOT STATE =
‘Maharashtra’;

------------------------------------------------------------------------------------------------------------------------------

B) Creation of table PRODUCT_MASTER

Step 1:

Table Name: PRODUCT_MASTER

CREATE TABLE PRODUCT_MASTER(PRODUCTNO varchar2(6),


DESCRIPTION varchar2(15),
PROFITPERCENT number(4,2),
UNITMEASURE varchar2(10),
QTYONHAND number(8),
REORDERLVL number(8),
SELLPRICE number(8,2),
COSTPRICE number(8,2));
Step 2:

Inserting records into the table:

INSERT INTO Product_Master VALUES('P00001','T-Shirts',5,'Piece',200,50,350,250);

INSERT INTO Product_Master VALUES('P03453','Shirts',6,'Piece',150,50,500,350);

INSERT INTO Product_Master VALUES('P06734','Cotton Jeans',5,'Piece',100,20,600,450);

INSERT INTO Product_Master VALUES('P07865','Jeans',5,'Piece',100,20,750,500);

------------------------------------------------------------------------------------------------------------

6
C) Creation of Table Salesman_master:

CREATE TABLE SALESMAN_MASTER(


SALESMAN_NO CHAR(6),
SALESMANNAME VARCHAR2(20),
ADDRESS1 VARCHAR2(30),
ADDRESS2 VARCHAR2(30),
CITY VARCHAR2(20),
PINCODE NUMBER(6),
STATE VARCHAR2(20),
SALAMT NUMBER(8,2),
TGTTOGET NUMBER(6,2),
YTDSALES NUMBER(6,2),
REMARKS VARCHAR2(60));

INSERT THE FOLLOWING DATA INTO THE TABLE:


SALESMAN SALESMANN ADDRE ADDRE CITY PINCO STATE SALA TGTTO YTDSA REMAR
_NO AME SS1 SS2 DE MT GET LES KS
S00001 Aman A/14 Worli Mum 4000 Maharas 3000 100 50 Good
bai 02 htra
S00002 Omkar 65 Narim Mum 4000 Maharas 3000 200 100 Good
an bai 01 htra
S00003 Raj P-7 Bandr Mum 4000 Maharas 3000 200 100 Good
a bai 32 htra
S00004 Ashish A/5 Juhu Mum 4000 Maharas 3500 200 150 Good
bai 44 htra

7
Sample Queries:

***

Common questions

Powered by AI

The TRUNCATE command in SQL is used to remove all records from a table while preserving the table structure. It is a DDL operation, hence faster because it does not generate individual row delete logs but rather logs the deallocation of the table's data pages. The DELETE command, a DML operation, removes records one at a time and logs each row deletion, thus enabling rollback and trigger operations but is slower compared to TRUNCATE .

INSERT statements are essential for adding new data into a database's tables, allowing for the growth and updating of dynamic datasets. It is fundamental within the DML category for populating a database with relevant information. Improper usage, such as syntax errors, can lead to incomplete data entries, while inserting duplicates could violate unique constraints, leading to data redundancy and inconsistencies that affect data integrity and queries .

ALTER TABLE is used when there is a need to modify an existing table structure, such as adding, removing columns, or changing data types. Typical scenarios include adding a new field to capture additional data, resizing a column to accommodate larger data, or renaming a column for better clarity. Incorrect use can lead to data inconsistencies or loss (e.g., removing a column with data), application errors if schema changes are not synchronized with application queries, and potential downtime if mismanagement leads to longer schema migration processes .

CREATE and DROP statements in SQL are antonyms in function; while CREATE is used to establish a new database object such as a table or view, DROP is used to completely remove it. These actions directly change the database schema—the CREATE command expands it by adding new structures, whereas DROP contracts it by eliminating structures, and losing all contained data without the possibility of recovery .

COMMIT, ROLLBACK, and SAVEPOINT commands enhance transaction management by ensuring data integrity and consistency. COMMIT finalizes a transaction, making all changes permanent. ROLLBACK undoes changes since the last COMMIT, useful in error handling and maintaining data integrity. SAVEPOINT allows setting intermediate points within a transaction to which you can ROLLBACK, providing granular control for managing complex transactions by partially undoing changes without affecting the entire transaction .

To increase the precision of a numerical column in a SQL table, you would use the ALTER TABLE command with the MODIFY clause. For example, you might use the syntax: ALTER TABLE <table_name> MODIFY <column_name> NUMBER(new_precision). A consideration is that the column should not contain any data entries that do not fit the new precision constraints. If decreasing precision, the column should ideally be empty to avoid loss of data .

When using GRANT and REVOKE commands in SQL's DCL category, it is crucial to consider the principle of least privilege, ensuring users have only the necessary permissions to perform their tasks to minimize security risks. Overuse of GRANT can expose sensitive data, while improper REVOKE operations can hinder legitimate access and disrupt system functionality. It's also important to maintain an organized record of assigned permissions and regularly review them to adapt to changing user roles and security audits .

The RENAME statement is used to change the name of existing database objects such as tables or columns. This can be strategic when refactoring or rebranding database systems, ensuring that names accurately reflect the current purpose or business logic without altering the data structure or integrity. It assists in maintaining a clear, organized schema over time as the table functions evolve .

Understanding the categories of SQL—DDL, DML, DRL, TCL, and DCL—is crucial for database management and operation. DDL governs the structural design, creating, altering, and dropping tables; DML modifies data via insertions and deletions; DRL retrieves data; TCL manages transactions ensuring data integrity during multiple operations with commands like COMMIT and ROLLBACK; DCL controls access permissions with GRANT and REVOKE. These categories interact by allowing comprehensive data management, from structure creation to data manipulation and access control, thus ensuring effective database operations .

DDL (Data Definition Language) in SQL is used to define and modify the database structure, such as creating, altering, or dropping tables. It handles the schema and structural integrity of the database without manipulating the data within the tables. In contrast, DML (Data Manipulation Language) is used for data manipulation within the database, including inserting, updating, or deleting records. DDL changes what tables exist, while DML changes the data within these tables .

You might also like