Data Definition in MySQL
• CREATE DATABASE: Creates a new database.
To create a database named students_xi_ip:
CREATE DATABASE students_xi_ip;
• CREATE TABLE: Creates a new table.
To create a table named marks:
CREATE TABLE marks
(Student_id char(5) primary key, sname varchar(30) not null, marks_acc int,
marks_IP int, discipline varchar(15) default “unknown”);
• DROP: Deletes a database or a table. It finishes the existence of the whole table or
database.
To delete a table named marks: DROP TABLE marks;
To delete a database named students_xi_ip: DROP DATABASE students_xi_ip;
• ALTER: Modifies an existing database object.
Adding an Attribute:
To add an attribute Grade_GK of datatype char(1) in the table marks:
ALTER TABLE marks ADD Grade_GK char(1);
Removing an Attribute:
To remove the attribute Grade_GK from the table marks:
ALTER TABLE marks DROP Grade_GK;
Removing the Primary Key:
To remove the primary key from the table marks:
ALTER TABLE marks DROP PRIMARY KEY;
Adding Primary Key:
To make the attribute student_id primary key of the table marks:
ALTER TABLE marks ADD PRIMARY KEY (student_id);
Adding UNIQUE constraint to an Attribute:
To add UNIQUE constraint to the attribute student_id of table marks:
ALTER TABLE marks ADD UNIQUE(student_id);
Modify Datatype of Attribute:
To change the data type of attribute sname to VARCHAR(40):
ALTER TABLE marks MODIFY sname VARCHAR(40) NOT NULL;
Modify Constraint of Attribute:
To add NOT NULL constraint to the attribute marks_acc of the table marks:
ALTER TABLE marks MODIFY marks_acc int NOT NULL;
Adding Default Value to an Attribute:
To add default value ‘Unknown’ to the attribute sname of table marks:
ALTER TABLE marks modify sname varchar(40) DEFAULT 'Unknown';
Data Query in MySQL
• SELECT: Retrieves data from the database. FROM: Specifies the table to query data
from.
To display the entire table marks: SELECT * FROM marks;
To display specific attributes student_id, sname from the table marks:
SELECT student_id, sname FROM marks;
Relational Operators in SQL:
Equal to (=), Not equal to (<> or !=), Greater than (>), Less than (<), Greater than or
equal to (>=), Less than or
equal to (<=).
Logical Operators in SQL:
Logical and (AND), Logical or (OR), Logical not (NOT).
• WHERE: Filters records based on specified conditions.
To display the details of students with marks_IP 80 or more:
SELECT * FROM marks WHERE marks_IP >= 80;
To display the student_id and sname of students who got “Good” in discipline:
SELECT student_id, sname FROM marks WHERE discipline = 'Good';
To display the details of students who did not got “Bad” in discipline:
SELECT * FROM marks WHERE discipline != 'Bad';
To display the details of students who got 90 or more in marks_Acc and “Outstanding”
in discipline:
SELECT * FROM marks WHERE marks_Acc >= 90
AND discipline = 'Oustanding';
• BETWEEN: Filters records within a range.
To display the details of students who got marks_IP in the range 70 to 100:
SELECT * FROM marks WHERE marks_IP BETWEEN 70 AND 100;
• IS NULL / IS NOT NULL: Checks for NULL/ NOT NULL values.
To display the details of student whose marks_Acc entry in the table is NULL:
SELECT * FROM marks WHERE marks_Acc IS NULL;
Data Manipulation in MySQL
• INSERT: Adds new records to a table.
[Considering the table marks has the attributes student_id, sname, marks_acc,
marks_IT, discipline]
To insert record having all the attributes:
INSERT INTO marks VALUES ('S0019', 'Sarojini Lakra', 85, 90, ’Very Good’);
To insert record not having all the attributes:
INSERT INTO marks (student_id, sname, marks_acc, discipline) VALUES ('S0100',
'Sourav Das', 70, ‘Good’);
• DELETE: Removes records from a table based on condition. If any condition is not
provided, it removes all the records from a table. It does not finish the existence of the
table.
To delete record of student having id S0100 from the table marks:
DELETE FROM marks WHERE student_id = 'S0100';
• UPDATE: Modifies existing records.
To modify the marks_acc of student having student_id S0019 to 88 in the table marks:
UPDATE marks SET marks_acc = 88 WHERE student_id = 'S0019';
Difference between DROP, DELETE & TRUNCATE:
Parameter DROP DELETE TRUNCATE
Language Data Definition Language Data Manipulation Data Definition
Command (DDL) Language Command Language Command
(DML) (DDL)
Purpose Used to delete records in a Used to delete records Used to delete all the
table along with the from a table (either records from a table,
structure of the table, specific records based on keeping the structure
finishing the existence of conditions or all the of the table.
the table. records), keeping the
structure of the table.