2. SQL - Data Definition Language
2. SQL - Data Definition Language
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;
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;
Operating System
Hardware
Swiggy
9606060871 / 9606060872
ARC Technologies & Institutions
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
Eg 2 : char(10) C
Eg 3 : char(5) C H I N K I
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
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
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
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.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.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
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.
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
9606060871 / 9606060872
ARC Technologies & Institutions
1) Date
Syntax : Date
A date. Format: YYYY-MM-DD
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
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.
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 :
SAL :
EMPNO ENAME JOB SAL DEPTNO
Dept No : 7369 SMITH CLERK 800 20
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 :
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
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)
9606060871 / 9606060872
ARC Technologies & Institutions Constraints
CONSTRAINT 6 : DEFAULT
Default ‘value’
9606060871 / 9606060872
ARC Technologies & Institutions
Example : Example :
CREATE INDEX index_EMPID CREATE UNIQUE INDEX index_EMPID
ON Employee (EMPNAME); ON Employee (EMPNAME);
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
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;
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;
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
9606060871 / 9606060872
ARC Technologies & Institutions
9606060871 / 9606060872
ARC Technologies & Institutions
9606060871 / 9606060872
ARC Technologies & Institutions
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);
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);
9606060871 / 9606060872
ARC Technologies & Institutions
5) CHECK
9606060871 / 9606060872
ARC Technologies & Institutions
4) RENAME TABLE
Syntax : RENAME table old_table_name TO new_table_name;
Example 2 : Renaming the table name from Employee to Student Using Alter
9606060871 / 9606060872
ARC Technologies & Institutions
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.
9606060871 / 9606060872
ARC Technologies & Institutions
9606060871 / 9606060872