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

2. SQL - Data Definition Language

The document provides an overview of SQL commands and data types used in database management, specifically focusing on Data Definition Language (DDL) commands such as CREATE, DROP, and ALTER. It details various data types including string, numeric, and binary types, along with their syntax and usage examples. Additionally, it includes specific instructions for creating and managing databases and tables within a relational database management system (RDBMS).

Uploaded by

Suyog Igave
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

2. SQL - Data Definition Language

The document provides an overview of SQL commands and data types used in database management, specifically focusing on Data Definition Language (DDL) commands such as CREATE, DROP, and ALTER. It details various data types including string, numeric, and binary types, along with their syntax and usage examples. Additionally, it includes specific instructions for creating and managing databases and tables within a relational database management system (RDBMS).

Uploaded by

Suyog Igave
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Technologies & Institutions

ARC Software Development And Software Testing Training with Placements Institute

WELCOME TO SQL
SESSION
Address
ARC Technologies and Institute ,NIRMAN ALTIUS, 1,
Mundhwa - Kharadi Rd, behind RADISSON BLU,
Pandhari Nagar, Kharadi, Pune, Maharashtra 411014 9606060871 / 9606060872
ARC Technologies & Institutions DATA DEFINITION LANGUAGE

1) CREATE DATABASE
1.1) CREATE TABLE

2) DROP DATABASE
3) ALTER TABLE
4) RENAME TABLE
4.1) RENAME COLUMN
5) TRUNCATE TABLE
ARC Technologies & Institutions

1) CREATE DATABASE
Question Two : Create Student Data Base
Syntax : create database databasename;
Question Three : Create Ola Data Base
Question One : Create Employee Data Base
Question Four : Create Swiggy Data Base
Answer : Create database Employee;

Create database Employee;


RDBMS

Operating System

C:
Hardware
Employee

9606060871 / 9606060872
ARC Technologies & Institutions

2) DROP DATABASE
Syntax : drop database databasename;
Question One : Drop Swiggy Data Base
Answer : Drop database Swiggy;

drop database swiggy;


RDBMS

Operating System

Hardware
Swiggy

9606060871 / 9606060872
ARC Technologies & Institutions

1.1) CREATE TABLE


Syntax : CREATE TABLE table_name (
column1 datatype,
EMP NO ENAME JOB SAL COMM DEPTNO DNAME LOC
column2 datatype,
101 SMITH CLERK 800 20 RESEARCH DALLAS
column3 datatype,
.... 102 ALLEN MANAGER 3459 1400 20 RESEARCH DALLAS
columnN datatype, 103 SCOTT ANALYST 3000 20 RESEARCH DALLAS
); 104 FORD SALESMAN 2450 0 20 RESEARCH DALLAS

What is datatype ???


Data Type indicates which type of data and range of value a column can hold .
Data Types in MySQL is of three types
1) Strings
2) Numeric
3) Date / Time

9606060871 / 9606060872
ARC Technologies & Institutions String Data Types

1) char
Syntax : char(size)
1) char data type is used to store the characters. such as ‘Ato Z' , 'a to z' ,'!,@, #,$.' , ' 0 to 9’ .
2) Fixed-length String that is always right-padded with spaces to the specified length.
3) Size indicates length which means maximum number of characters a column can hold.
4) The Range of Size is 0 to 255
Eg 1 : char(10) C H I N K I

Used Memory UnUsed Memory / Wastage of Memory

Eg 2 : char(10) C

Used Memory UnUsed Memory / Wastage of Memory

Eg 3 : char(5) C H I N K I

Used Memory Lost Data

Eg 4 : char Note : By Default char will be 1

9606060871 / 9606060872
ARC Technologies & Institutions String Data Types

2) varchar
Syntax : varchar(size)
1) varchar data type is used to store the characters. such as ‘Ato Z' , 'a to z' ,'!,@, #,$.' , ' 0 to 9’ .
2) Variable-length String that is always right-padded stores only the character inserted.
3) Size indicates length which means maximum number of characters a column can hold.
4) The Range of Size is 0 to 255
Eg 1 : varchar(10) C H I N K I

Used Memory Unused Memory will be deleted

Eg 2 : varchar(10) A R C T E C H N O L O G I E S
Error : Data too Long for column.

Eg 3 : varchar(5) C

Used Memory Memory will not be taken

Eg 4 : varchar Note : No Default -> Size is Mandatory

9606060871 / 9606060872
ARC Technologies & Institutions String Data Types

3) text
Syntax : text(size) Note : Size is optional and Default Size is 1
1) text data type is used to store the characters. such as 'a to z' , ‘A to Z' ,'!,@, #,$.' , ' 0 to 9’ .
2) Variable-length String that is always right-padded stores only the character inserted.
3) Size indicates length which means maximum number of characters a column can hold.
4) The Range of Size is 0 to 65535 bytes = 64 KB

1 bit 8 bits = 1 byte 1024 bytes = 1 kilobyte (KB) 1024 kilobyte = 1 megabyte (MB) 1024 megabyte = 1 Gigabyte (GB) 1024 Gigabyte = 1 terabyte (TB)

9606060871 / 9606060872
ARC Technologies & Institutions String Data Types

3.1) tinytext
1) tinytext data type is used to store the characters. such as 'a to z' , ‘A to Z' ,'!,@, #,$.' , ' 0 to 9’ .
2) The Range of Size is 255 characters
Syntax : tinytext Note : we cannot define size, hence by default it is 255
Eg 1 : tinytext(10) Error : Cannot define size
3.2) mediumtext
1) mediumtext data type is used to store the characters. such as 'a to z' , ‘A to Z' ,'!,@, #,$.' , ' 0 to 9’ .
2) The Range of Size is 16777215 characters
Syntax : mediumtext Note : we cannot define size, hence by default it is 16777215
Eg 1 : mediumtext(10) Error : Cannot define size
3.3) longtext
1) mediumtext data type is used to store the characters. such as 'a to z' , ‘A to Z' ,'!,@, #,$.' , ' 0 to 9’ .
2) The Range of Size is 4294967295 characters
Syntax : longtext Note : we cannot define size, hence by default it is 4294967295
Eg 1 : longtext(10) Error : Cannot define size
9606060871 / 9606060872
ARC Technologies & Institutions String Data Types

4) blob
Syntax : blob(size) Blob : Binary Large Object
1) It is used to store the binary values of audio,video,images,files etc.
2) The Range of Size is 0 to 65535 bytes = 64 KB

Note : Default Size is 1

1 0 0 1 0 0 0 0 0 1
1 1 1 1 0 0 0 0 0 0
0 0 1 1 0 1 0 0 1 0
1 0 0 1 0 0 0 0 0 1
1 1 1 1 0 0 0 0 0 0
0 0 1 1 0 1 0 0 1 0
0 1 1 0 1 0 0 1 1 1
1 1 0 1 1 0 1 0 0 1

9606060871 / 9606060872
ARC Technologies & Institutions String Data Types

3.1) tinyblob
1) It is used to store the binary values of audio,video,images,files etc.
2) The Range of Size is 255 bytes
Syntax : tinyblob Note : we cannot define size, hence by default it is 255 bytes

3.2) mediumblob
1) It is used to store the binary values of audio,video,images,files etc.
2) The Range of Size is 16777215 bytes (16MB)
Syntax : mediumblob Note : we cannot define size, hence by default it is 16777215 bytes
3.2) largeblob
1) It is used to store the binary values of audio,video,images,files etc.
2) The Range of Size is 4294967295 bytes (4GB)
Syntax : largeblob Note : we cannot define size, hence by default it is 4294967295 bytes

9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

Numeric Point 1 : Both Integer and Floating values have + and – values
Point 2 : All the values without decimal point is called as Integer values.
Point 3 : All the values with decimal point is called as floating values.
Integer Floating
Eg : +12 Eg : -12.23 Test :
-13 +13.45 34.89 ->
-3 -3.98
+7.55 46 ->
-7
+18 -18.99 0.11 ->

0 ->
0.001 ->

9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

1) int
Note 1 : Size is optional and Default Size is 10
Syntax : int(size) Note 2 : By default it will be signed.
1) Int holds normal integer value.
2) Signed range is from -2147483648 to 2147483647
3) Unsigned range is from 0 to 4294967295

Eg 1 : int unsigned 4 2 9 4 9 6 7 2 9 5

Eg 2 : int signed
First Bit is used to store Sign

1.1) integer Note 1 : Size is optional and Default Size is 10


Syntax : integer(size) Note 2 : By default it will be signed.
1) Int Can also be written as Integer
9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

1.2) tinyint
Note : By default it will be signed.
Syntax : tinyint(size)
1) tinyInt holds normal integer value.
2) Signed range is from -128 to 127
3) Unsigned range is from 0 to 255
1.3) smallint Note : By default it will be signed.
Syntax : smallint(size)
1) smallint holds normal integer value.
2) Signed range is from -32768 to 32767
3) Unsigned range is from 0 to 65535

1.4) mediumint Note : By default it will be signed.


Syntax : mediumint(size)
1) mediumint holds normal integer value.
2) Signed range is from -8388608 to 8388607
3) Unsigned range is from 0 to 16777215
9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

1.2) bigint
Syntax : bigint(size)
1) bigint holds normal integer value.
2) Signed range is from -9223372036854775808 to 9223372036854775807
3) Unsigned range is from 0 to 18446744073709551615

Summary Table : Signed Unsigned


Name Syntax Lower Higher Lower Higher
tinyint tinyint(size) -128 127 0 255
smallint Smallint(size) -32768 32767 0 65535
mediumint Mediumint(size) -8388608 8388607 0 16777215
int Int(size) -2147483648 2147483647 0 4294967295
integer Integer(size) -2147483648 2147483647 0 4294967295
bigint bigint(size) -9223372036854775808 9223372036854775807 0 18446744073709551615

Note 1 : all Size is optional & By default it will be signed.


9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

1) Float
Syntax : float(size,d)
1) Indicates a Single precision floating point value
2) Size indicates length of the number can hold.
3) D indicates the number of decimals.
4) By default it will be signed.
Eg 1 : float (5,3) -> 12.456
Eg 2 : float(7,2) -> 12345.12
Eg 3. float (8,5) 1132.12345675 -> 1132.12356

Syntax : float(p)
1. Where p is optional, Default is 24.
2. where p is used only to determine storage size.
3. A Precision from 0 to 23 results.
Eg 1 : float (8) -> 1.1234567
Eg 2 : float(7) -> 123.4567
9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

2) Double
Syntax : double(size,d)
1) Size specifies the total number of digits.
2) The d indicates the number of decimal values.
Note 1 : Size is optional and Default Size is 16,4
Note 2 : By default it will be signed.
Eg 1 : double (5,3) -> 12.456
Eg 2 : double(7,2) -> 12345.12

Syntax : double(p)
1. Where P is optional and default size is 54.
2. where p is used only to determine storage size.
3. A Precision from 24 to 53 results.

Eg 1 : double (12) -> 1.12345678901

9606060871 / 9606060872
ARC Technologies & Institutions Numeric Data Types

3) Decimal
Syntax : decimal(size,d)
1) Indicates a Single precision floating point value 3) The maximum number for size is 65 and d is 30.
2) Size indicates length of the number can hold. 4) The default value for size is 10 and for d is 0.
Eg 1 : decimal (5,3) -> 12.456 -> 12.456
Eg 2 : decimal(7,2) -> 12345.12 -> 12345.12
Syntax : decimal
Note 1 : By default it will be signed.
Eg 1 : decimal -> 12.456 ->12
Eg 2 : decimal -> 12345.22 -> 12345
Eg 3 : decimal -> 123456.22 -> 123456
Eg 4 : decimal -> 123456789.123456789123456789 -> 123456789

Note 1 : If Size and d is not mentioned, we will not get value after decimal point.

9606060871 / 9606060872
ARC Technologies & Institutions

4) Numeric
Syntax : numeric(size)
Eg 1 : numeric (5) -> 124567 -> 12456
Eg 2 : numeric (8) -> 12345678 -> 12345678
Syntax : numeric(size,d)
Eg 1 : numeric(5,3) -> 12.456 ->12.456
Eg 2 : numeric(10,3) -> 1234567.222 -> 1234567.222

Note 1 : Numeric can be used for both.

9606060871 / 9606060872
ARC Technologies & Institutions

1) Date
Syntax : Date
A date. Format: YYYY-MM-DD

Example : 12 / Dec / 2022 Saved and Displayed : 2022 - 12 - 11

2) DATETIME
Syntax : DateTime
A date and time combination. Format: YYYY-MM-DD hh:mm:ss.

Example : 12 / Dec / 2022 21:22:59 Saved and Displayed : 2022 -12-11 21:22:59

3) TIME
Syntax : Time
A time. Format: hh:mm:ss.

9606060871 / 9606060872
ARC Technologies & Institutions

SQL constraints are used to specify rules for data in a table.


Syntax :

CREATE TABLE table_name (


column1 datatype constraint,
column2 datatype constraint, Data Type Integer Varchar Varchar Int Int
column3 datatype constraint, NotNull NotNull NotNull NotNull
.... Constraints
); Unique Sal >0

EMPNO ENAME JOB SAL DEPTNO


7369 SMITH CLERK 800 20

7499 ALLEN SALESMAN 1600 30

7521 WARD SALESMAN 1250

7566 JONES MANAGER 2975 20

7654 MARTIN SALESMAN 1250

7698 BLAKE MANAGER 2850 30

9606060871 / 9606060872 7782 CLARK MANAGER 2450


ARC Technologies & Institutions Constraints
CONSTRAINT 1 : NOTNULL

NULL: Null is a keyword which is used to represent an empty cell or nothing.


CHARACTERISTICS OF NULL:
1. Any operations performed with null resultant will be null
2. In RDBMS two null values can not be same.

NOTNULL: It is used to represent the cell should not be empty or null

Example :
Emp ID :
CREATE TABLE Employee (
Emp Name : EMPID int NOT NULL,
EMPName
JOB :
varchar(255) NOT NULL,
SAL : JOB varchar(255) NOT NULL,
SAL int NOT NULL,
Dept No : DeptNo int
);
Submit Cancel
9606060871 / 9606060872
ARC Technologies & Institutions Constraints
CONSTRAINT 2 : UNIQUE

UNIQUE : It is used to avoid duplicate values which are entered into the cell.

Example 1 : If there is only one Unique Attribute in the table


CREATE TABLE Employee (
Emp ID : EMPID int NOT NULL Unique,
EMPName varchar(255) NOT NULL,
Emp Name : JOB varchar(255) NOT NULL,
SAL int NOT NULL,
JOB : DeptNo int
);
SAL :
Example 2 : If there are more than one Unique Attribute in the table
Dept No :
CREATE TABLE Employee (
Submit Cancel EMPID int NOT NULL,
EMPName varchar(255) NOT NULL,
JOB varchar(255) NOT NULL,
SAL int NOT NULL,
DeptNo int,
Unique(EMPID, DeptNo)
); 9606060871 / 9606060872
ARC Technologies & Institutions Constraints

Emp ID : Example 3 : Combining two column to make it as unique


Emp Name : CREATE TABLE Persons (
EMPID int NOT NULL,
JOB : EMPName varchar(255) NOT NULL,
JOB varchar(255) NOT NULL,
SAL : SAL int NOT NULL,
DeptNo int,
Dept No : Constraint UNIQUE_Per Unique (EMPID, DeptNo)
);
Submit Cancel

9606060871 / 9606060872
ARC Technologies & Institutions Constraints
CONSTRAINT 3 : PRIMARY KEY
PRIMARY KEY: It is used to uniquely identify records from the table.
• The table can have only one primary key.
• It should be unique.
• It should be not null.
• It is always a combination of unique and not null.
• It is not mandatory, but it is recommended.
• In a table there can be multiple columns which are eligible to become a primary key, but we
will select only one column as primary key.

Emp ID :
Example 1 :
Emp Name :
CREATE TABLE Employee (
EMPID int NOT NULL Primary key,
JOB : EMPName varchar(255) NOT NULL,
JOB varchar(255) NOT NULL,
SAL :
SAL int NOT NULL,
Dept No : DeptNo int
);
Submit Cancel
9606060871 / 9606060872
ARC Technologies & Institutions Constraints

Example 1 :

CREATE TABLE Persons (


EMPID int NOT NULL Primary key,
EMPName varchar(255) NOT NULL,
Emp ID :
JOB varchar(255) NOT NULL,
Emp Name : SAL int NOT NULL,
DeptNo int
);
JOB :

SAL :
EMPNO ENAME JOB SAL DEPTNO
Dept No : 7369 SMITH CLERK 800 20

7499 ALLEN SALESMAN 1600 30


Submit Cancel
7521 WARD SALESMAN 1250

7566 JONES MANAGER 2975 20

7654 MARTIN SALESMAN 1250

7698 BLAKE MANAGER 2850 30

7782 CLARK MANAGER 2450


9606060871 / 9606060872
ARC Technologies & Institutions Constraints

Example 2 :
CREATE TABLE Persons (
EMPID int NOT NULL ,
EMPName varchar(255) NOT NULL,
JOB varchar(255) NOT NULL,
SAL int NOT NULL,
Emp ID : DeptNo int,
Primary key (EMPID)
Emp Name : );

JOB :

SAL : EMPNO ENAME JOB SAL DEPTNO


7369 SMITH CLERK 800 20
Dept No :
7499 ALLEN SALESMAN 1600 30

Submit Cancel 7521 WARD SALESMAN 1250

7566 JONES MANAGER 2975 20

7654 MARTIN SALESMAN 1250

7698 BLAKE MANAGER 2850 30

7782 CLARK MANAGER 2450


9606060871 / 9606060872
ARC Technologies & Institutions Constraints
Example 3 :
CREATE TABLE Persons (
EMPID int NOT NULL ,
EMPName varchar(255) NOT NULL,
JOB varchar(255) NOT NULL,
SAL int NOT NULL,
DeptNo int,
Emp ID : Constraint Person_ID Primary key (EMPID, EMPName)
);
Emp Name :

JOB :
Person_ID EMPNO ENAME JOB SAL DEPTNO
7369 SMITH CLERK 800 20
SAL :
7499 ALLEN SALESMAN 1600 30
Dept No : 7521 WARD SALESMAN 1250

7566 JONES MANAGER 2975 20


Submit Cancel
7654 MARTIN SALESMAN 1250

7698 BLAKE MANAGER 2850 30

7782 CLARK MANAGER 2450

Note : Combination of EMPNO and ENAME will be primary key


9606060871 / 9606060872
ARC Technologies & Institutions Constraints

CONSTRAINT 4 : FOREIGN KEY


FOREIGN KEY : IT IS USED TO ESTABLISH THE CONNECTION BETWEEN ANY TWO TABLES.

• A Table can have any number of foreign key.


• It can accept duplicate values.
• It can have null values.
• It is not a combination of unique and not null.
• Primary key of the table can only become foreign key of another table.
• Foreign key is always present in child table, but actually belongs to parent table.
• Foreign key is also known as referential integrity constraint.

9606060871 / 9606060872
ARC Technologies & Institutions Constraints
Employee PK FK Department
EMPNO ENAME JOB SAL DEPTNO
7369 SMITH CLERK 800 20
PK
7499 ALLEN SALESMAN 1600 30
DEPTNO DNAME LOC
7521 WARD SALESMAN 1250 10 ACCOUNTING NEW YORK
7566 JONES MANAGER 2975 20 20 RESEARCH DALLAS
30 SALES CHICAGO
7654 MARTIN SALESMAN 1250
40 OPERATIONS BOSTON
7698 BLAKE MANAGER 2850 30
7782 CLARK MANAGER 2450

Example 1 :
CREATE TABLE Employee (
EMPID int NOT NULL ,
EMPName varchar(255) NOT NULL,
JOB varchar(255) NOT NULL,
SAL int NOT NULL,
DeptNo int,
foreign key (Deptno) references Department(DeptNo)
);
9606060871 / 9606060872
ARC Technologies & Institutions Constraints

CONSTRAINT 5 : CHECK
CHECK: It is used to provide an extra or additional condition to validate the data

SYNTAX : CHECK(CONDITION)

EMPNO ENAME JOB SAL DEPTNO


7369 SMITH CLERK 800 20
Example 1 :
7499 ALLEN SALESMAN 1600 30
CREATE TABLE Employee (
7521 WARD SALESMAN 1250 EMPID int NOT NULL check(length==4) ,
7566 JONES MANAGER 2975 20 EMPName varchar(255) NOT NULL,
7654 MARTIN SALESMAN 1250 JOB varchar(255) NOT NULL,
SAL int NOT NULL,
7698 BLAKE MANAGER 2850 30
DeptNo int,
7782 CLARK MANAGER 2450 foregin key (Deptno) references Department (DeptNo)
);

9606060871 / 9606060872
ARC Technologies & Institutions Constraints

CONSTRAINT 6 : DEFAULT

DEFAULT : Constraint is used to set a default value for a column.

Default ‘value’

EMPNO ENAME JOB SAL DEPTNO


7369 SMITH CLERK 800 20
Example 1 :
CREATE TABLE Employee (
7499 ALLEN SALESMAN 1600 30
EMPID int NOT NULL check(length==4) ,
7521 WARD SALESMAN 1250 0 EMPName varchar(255) NOT NULL,
7566 JONES MANAGER 2975 20 JOB varchar(255) NOT NULL,
7654 MARTIN SALESMAN 1250 0 SAL int NOT NULL,
DeptNo int, default ‘0’
7698 BLAKE MANAGER 2850 30
);
7782 CLARK MANAGER 2450 0

9606060871 / 9606060872
ARC Technologies & Institutions

CONSTRAINT : CREATE INDEX

CREATE INDEX : statement is used to create indexes in tables.


NOTE : Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the
indexes, they are just used to speed up searches/queries.
Syntax 1 : To Create Index Syntax 2 : To Create Unique Index

CREATE INDEX index_name CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, ...); ON table_name (column1, column2, ...);

Example : Example :
CREATE INDEX index_EMPID CREATE UNIQUE INDEX index_EMPID
ON Employee (EMPNAME); ON Employee (EMPNAME);

Search Query : Select * from Employee where ENAME =‘Saurabh Navadkar’

9606060871 / 9606060872
EMPLOYEE TABLE INDEX TABLE
EMP NAME Pointer EMP NAME Pointer
Pratiksha Sangale P_1 Aishwarya Nanaji Awari P_13
Samadhan dattatray sonvane P_2 Ashutosh Ghuse P_6
Lalita Pandurang Jadhav P_3 Ayush Nandankar P_10
Ingole Ankita sanjay P_4 Ingole Ankita sanjay P_4
Ranjit Vitthal Aiwale P_5 Kajal Ravsaheb Jengate P_8
Ashutosh Ghuse P_6 Lalita Pandurang Jadhav P_3
Rohan Sanjay Mohite P_7 Megha Digambar Mattamwad P_17
Kajal Ravsaheb Jengate P_8 Pradnya Suresh Patil P_19
Saurabh Navadkar P_9 Prajwal Prakash Chavan P_15
Ayush Nandankar P_10 Pratiksha Sangale P_1
Pravin Mane P_11 Pravin Mane P_11
Sakharam Sambhaji Patil P_12 Ranjit Vitthal Aiwale P_5
Aishwarya Nanaji Awari P_13 Rohan Sanjay Mohite P_7
Sourabh Bhikaji Lambore P_14 Sahil Dayanand Ade P_16
Prajwal Prakash Chavan P_15 Sakharam Sambhaji Patil P_12
Sahil Dayanand Ade P_16 Samadhan dattatray sonvane P_2
Megha Digambar Mattamwad P_17 Saurabh Navadkar P_9
Swapnil Chaudhary P_18 Sourabh Bhikaji Lambore P_14
Pradnya Suresh Patil P_19 Swapnil Chaudhary P_18
ARC Technologies & Institutions

1) Create Database Swiggy ?


Query : create database Swiggy;
2) Create table Department ?
Data Type numeric Varchar Varchar
Primary Key NOTNULL NOTNULL
Constraints

Column Name DEPTNO DNAME LOC

Query : create table Department(DEPTNO numeric(4) primary key, DNAME varchar(50) NOT NULL, LOC varchar(255) NOT NULL);
3) Create table Employee ?
Data Type numeric Varchar Varchar numeric Date numeric numeric numeric
Primary Key NOTNULL NOTNULL NOTNULL NOTNULL NOTNULL
Constraints
Sal > 0 Foreign Key
Column Name EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

Query :
create table Employee (EMPNO numeric(4) primary key, ENAME varchar(255) NOT NULL, JOB varchar(255) NOT NULL, MGR numeric(4), HIREDATE date
NOT NULL, SAL int NOT NULL , COMM numeric , deptno numeric(4), constraint foreign key(deptno) references Department(DEPTNO), check (SAL>0) );

9606060871 / 9606060872
ARC Technologies & Institutions

2) Drop Table
The DROP TABLE statement is used to drop and existing table in a database.
Syntax : DROP TABLE table_name;

Example : Drop Employee table from table;

EMPNO ENAME JOB SAL DEPTNO


7369 SMITH CLERK 800 20

7499 ALLEN SALESMAN 1600 30

7521 WARD SALESMAN 1250

Query :
DROP TABLE Employee;

9606060871 / 9606060872
ARC Technologies & Institutions

3) Alter Table
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table
The ALTER TABLE statement is also used to add and drop various constraints on an existing table
Adding Additional Column to the Existing Table :
Syntax : ALTER TABLE table_name
ADD column_name datatype;

Example : Add SAL Column into the existing column

EMPNO ENAME JOB SAL DEPTNO EMPNO ENAME JOB SAL DEPTNO SAL
7369 SMITH CLERK 800 20 7369 SMITH CLERK 800 20 800
7499 ALLEN SALESMAN 1600 30 7499 ALLEN SALESMAN 1600 30 1600
7521 WARD SALESMAN 1250 7521 WARD SALESMAN 1250 1250

Query : ALTER TABLE Employee


ADD SAL numeric;

9606060871 / 9606060872
ARC Technologies & Institutions

Modifing Column in the Existing Table :

Syntax : ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

Example : Modify SAL column datatype from int to float.

EMPNO ENAME JOB SAL DEPTNO SAL


7369 SMITH CLERK 800 20 800

7499 ALLEN SALESMAN 1600 30 1600

7521 WARD SALESMAN 1250 1250

Query : ALTER TABLE Employee


Modify COLUMN SAL float;

9606060871 / 9606060872
ARC Technologies & Institutions

Droping Additional Column from the Existing Table :

Syntax : ALTER TABLE table_name


DROP COLUMN column_name;

Example : Drop SAL Column from the existing column

EMPNO ENAME JOB SAL DEPTNO SAL


7369 SMITH CLERK 800 20 800

7499 ALLEN SALESMAN 1600 30 1600

7521 WARD SALESMAN 1250 1250

Query : ALTER TABLE Employee


DROP COLUMN SAL;

9606060871 / 9606060872
ARC Technologies & Institutions

Modifing Constraints in the Existing Table :


1) NOT NULL
Example 1 : Modifying NOT NULL Constraint
ALTER TABLE Employee
MODIFY DEPTNO int NOT NULL;
Note : Modification is only possible when there is no data added in the table

2) UNIQUE
Example 1 : Modifying UNIQUE Constraint
ALTER TABLE Employee
ADD UNIQUE(EMPID);
Note : Modification is only possible is there is no data added in the table

9606060871 / 9606060872
ARC Technologies & Institutions

3) PRIMARY KEY
Example 1 : Modifying PRIMARY KEY Constraint
ALTER TABLE Employee
ADD PRIMARY KEY (EMPID);
Example 2 : Modifying PRIMARY KEY Constraint on multiple columns
ALTER TABLE Employee
ADD constraint PK_person PRIMARY KEY (EMPID,ENAME);

Example 3 : Dropping PRIMARY KEY Constraint on multiple columns


ALTER TABLE Employee
DROP PRIMARY KEY;

9606060871 / 9606060872
ARC Technologies & Institutions

4) FOREIGN KEY
Example 1 : Modifying FOREIGN KEY Constraint
ALTER TABLE Employee
ADD FOREIGN KEY (EMPID) REFERENCES DEPARTMENT(Deptno);
Example 2 : Modifying FOREIGN KEY Constraint on multiple columns
ALTER TABLE Employee
ADD constraint FK_EMPID FOREIGN KEY (EMPID) REFERENCES DEPARTMENT(Deptno);

Example 3 : Dropping FOREIGN KEY Constraint on multiple columns


ALTER TABLE Employee
DROP FOREIGN KEY FK_EMPID;

9606060871 / 9606060872
ARC Technologies & Institutions

5) CHECK

Example 1 : Modifying CHECK Constraint


ALTER TABLE Employee
ADD CHECK (Age>=18);

9606060871 / 9606060872
ARC Technologies & Institutions

4) RENAME TABLE
Syntax : RENAME table old_table_name TO new_table_name;

Example 1 : Renaming the table name from Student to Employee


Query : Rename table Student to Employee;

Example 2 : Renaming the table name from Employee to Student Using Alter

Query : alter table Employee rename to Student;

9606060871 / 9606060872
ARC Technologies & Institutions

4.1) RENAME COLUMN


1.Using the CHANGE statement (MariaDB)
Syntax :
ALTER TABLE table_name
CHANGE COLUMN old_column_name to new_column_name;
Example 1 : alter table employee
change column sal to salary;
2. Using the RENAME statement (MYSQL & ORACLE)
Syntax :
ALTER TABLE table_name
RENAME COLUMN old_column_name new_column_name ;
Example 1 : alter table employee
rename column sal to salary ;

9606060871 / 9606060872
ARC Technologies & Institutions

5) TRUNCATE TABLE
The TRUNCATE statement in MySQL removes the complete data without
removing its structure
Syntax :
TRUNCATE [TABLE] table_name;
Example : truncate employee3;
Note : After truncate data will be removed not the table.

EMPNO ENAME JOB SAL DEPTNO


7369 SMITH CLERK 800 20
7499 ALLEN SALESMAN 1600 30
After truncate
7521 WARD SALESMAN 1250 0 EMPNO ENAME JOB SAL DEPTNO
7566 JONES MANAGER 2975 20
7654 MARTIN SALESMAN 1250 0
7698 BLAKE MANAGER 2850 30
7782 CLARK MANAGER 2450 0

9606060871 / 9606060872
ARC Technologies & Institutions

9606060871 / 9606060872

You might also like