0% found this document useful (0 votes)
22 views5 pages

Mysql Class 11

This document provides a comprehensive overview of MySQL, covering data types, constraints, and SQL commands including Data Definition Language (DDL) and Data Manipulation Language (DML). It includes examples for creating, modifying, and deleting databases and tables, as well as inserting, updating, and selecting data. Additionally, it highlights important SQL keywords and operators for data retrieval and manipulation.
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)
22 views5 pages

Mysql Class 11

This document provides a comprehensive overview of MySQL, covering data types, constraints, and SQL commands including Data Definition Language (DDL) and Data Manipulation Language (DML). It includes examples for creating, modifying, and deleting databases and tables, as well as inserting, updating, and selecting data. Additionally, it highlights important SQL keywords and operators for data retrieval and manipulation.
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/ 5

MYSQL SHORTNOTES

DATATYPES:
DATATYPES DETAILS EXAMPLE
Varchar(size) Character type. Don’t “Hello World”
forget to add “” or ‘’
Int Integer type 1,2,3,45,49…..
Float Decimal point s 2.43,6.78,4.23….
Date Date type. “2024-11-13”
Format: YYYY-MM-DD

CONSTRAITNS:
NOT NULL Ensures that a column cannot have
NULL values where NULL means
missing/ unknown/not applicable
value.
UNIQUE Ensures that all the values in a
column are distinct/unique.
DEFAULT A default value specified for the
column if no value is provided.
PRIMARY KEY The column which can uniquely
identify each row or record in a
table.
FOREIGN KEY The column which refers to value of
an attribute defined as primary key
in another table.

SQL COMMANDS:
1. Data Definition Language (DDL) commands.
2. Data Manipulation Language (DML) commands
3. Transaction Control Language (TCL) commands.

Prepared by Tanmay Biswas 1


DDL COMMANDS
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
CREATE:
To create database: CREATE DATABASE databasename;
Example: CREATE DATABASE Student;
For using the database: USE databasename;
Example: USE Student;
Lists names of all the tables within a database: SHOW TABLES;
To create table: CREATE TABLE tablename( attributename1 datatype constraint,
attributename2 datatype constraint, : attributenameN datatype constraint);
Example: CREATE TABLE STUDENT( RollNumber INT, SName VARCHAR(20),
SDateofBirth DATE, GUID CHAR(12), PRIMARY KEY (RollNumber));
To view the structure of an already created table: DESCRIBE tablename;

ALTER:
After creating a table, if we need to add/remove an attribute or to modify the datatype
of an existing attribute or to add constraint in attribute, we need to change or alter the
structure of the table by using the alter.
Syntax: ALTER TABLE tablename ADD/Modify/DROP attribute1, attribute2,..;
Add primary key to a relation: ALTER TABLE TABLE_NAME ADD PRIMARY KEY
(ATTRIBUTE NAME);
Example : ALTER TABLE Student ADD PRIMARY KEY (SID);
Add foreign key to a relation: Make sure –
• The referenced relation must be already created.
• The referenced attribute must be a part of primary key of the referenced relation.
• Data types and size of referenced and referencing attributes must be same.

Prepared by Tanmay Biswas 2


Syntax: ALTER TABLE table_name ADD FOREIGN KEY(attribute name) REFERENCES
referenced_table_name (attribute name);
Example : ALTER TABLE STUDENT ADD FOREIGN KEY(GUID) REFERENCES
GUARDIAN(GUID);
Add an attribute to an existing table: ALTER TABLE table_name ADD attribute_name
DATATYPE;
Example : ALTER TABLE Student ADD marks INT;
Modify datatype of an attribute: ALTER TABLE table_name MODIFY attribute
DATATYPE;
Example: ALTER TABLE Student MODIFY SName VARCHAR(40);
Remove an attribute: ALTER TABLE table_name DROP attribute;
Example : ALTER TABLE Student DROP marks;
Remove primary key from the table: ALTER TABLE table_name DROP PRIMARY KEY;
Example : ALTER TABLE Student DROP PRIMARY KEY;

DROP:
To delete a table: DROP TABLE table_name;
To delete a database: DROP DATABASE database_name;

DML COMMANDS
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database

Prepared by Tanmay Biswas 3


INSERT INTO:
Syntax: INSERT INTO tablename VALUES(value 1, value 2,....);
Example: INSERT INTO STUDENT VALUES (1, 'Alice Johnson', '2005-01-15',
'GUID123456');
If we want to provide values only for some of the attributes in a table then we shall
specify the attribute name alongside each data value:
INSERT INTO tablename (column1, column2, ...) VALUES (value1, value2, ...);
Example: INSERT INTO STUDENT (RollNumber, SName,GUID) VALUES (6, 'Frank Miller',
'GUID999888');

SELECT:
To retrieve specific data from table : SELECT attribute1, attribute2, ... FROM
table_name WHERE condition;
Example: SELECT SName, SDateofBirth FROM STUDENT WHERE RollNumber = 1;
To retrieve full table : SELECT * FROM table_name ;
Example: SELECT * FROM table_name ;

UPDATE :
To make changes in the value(s) of one or more columns of existing records in a
table : UPDATE table_name SET attribute1 = value1, attribute2 = value2, WHERE
condition;
Example : UPDATE STUDENT SET GUID = 101010101010 WHERE RollNumber = 3;

DELETE:
To delete one or more record(s) from a table : DELETE FROM table_name WHERE
condition;
Example: DELETE FROM STUDENT WHERE RollNumber = 2;

Prepared by Tanmay Biswas 4


Important keywords:
To show values without duplicate values use DISTINCT keyword.
Syntax : SELECT DISTINCT attribute_name FROM table_name;
Example :SELECT DISTINCT Sname FROM Student;
The BETWEEN operator in SQL is used to filter data within a specified range.
Syntax: SELECT attribute (s) FROM table_name WHERE attribute _name BETWEEN
value1 AND value2;
Example: SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND
'2023-12-31';
The IN operator compares a value with a set of values and returns true if the value
belongs to that set.
Syntax: SELECT attribute (s) FROM table_name WHERE attribute _name IN (val1, val2 ,
val3);
Example: SELECT * FROM EMPLOYEE WHERE DeptId IN ('D01', 'D02' , 'D04');
ORDER BY clause is used to display data in an ordered (arranged) form with respect
to a specified column.
Syntax: SELECT attribute (s) FROM table_name ORDER BY attribute;
Example :SELECT * FROM EMPLOYEE ORDER BY Salary;
For descending order : SELECT * FROM EMPLOYEE ORDER BY Salary DESC;
For ascending order : SELECT * FROM EMPLOYEE ORDER BY Salary ASC;
Substring pattern :
The LIKE operator makes use of the following two wild card characters:
• % (percent)— used to represent zero, one, or multiple characters
• _ (underscore)— used to represent a single character
Whose name starts with 'n'. : SELECT * FROM table WHERE attribute LIKE 'n%';(n
could be any char);
Whose name ends with 'n'. : SELECT * FROM table WHERE attribute LIKE '%n';(n
could be any char);
Whose name consists of exactly n value(let n=5) letters: SELECT * FROM table
WHERE attribute LIKE '_ _ _ _ _ '; (N.B:_ value and total letter values should be
same so that there are 5 _);

Prepared by Tanmay Biswas 5

You might also like