Mysql Class 11
Mysql Class 11
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.
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.
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
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;