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

MySQL_DAY_1

The document provides an overview of MySQL database management, focusing on various types of constraints (entity, referential, semantic) that ensure data integrity and relationships between tables. It details the different SQL languages (DDL, DML, TCL, DQL, DCL) and includes syntax for creating, modifying, and deleting database objects, as well as managing user permissions. Additionally, it covers interview questions related to constraints and data validation practices in MySQL.

Uploaded by

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

MySQL_DAY_1

The document provides an overview of MySQL database management, focusing on various types of constraints (entity, referential, semantic) that ensure data integrity and relationships between tables. It details the different SQL languages (DDL, DML, TCL, DQL, DCL) and includes syntax for creating, modifying, and deleting database objects, as well as managing user permissions. Additionally, it covers interview questions related to constraints and data validation practices in MySQL.

Uploaded by

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

• Data stored in

tables

MySQL • Rows hold on


individual records
CONSTRAINTS – ENTITY, REFERENTIAL, SEMANTIC
constraints are like rules that govern how data can be inserted, • Columns hold
updated, or deleted within a database. They act as safeguards
to ensure data integrity, consistency, and accuracy. different types of
data

• Can create
relationship
• DDL – Data
Definition Language
• DML – Data
Manipulation
Language
• TCL – Transaction
Control Language
• DQL – Data Query
Language
• DCL – Data Control
Language
CREATE:-
CREATE [TABLE | VIEW | INDEX | USER | ... ]
[name] (
[column1_name] [data_type] [constraint1] [constraint2],
[column2_name] [data_type] [constraint1] [constraint2],
...
DATA )

DEFINITION [... other options ...];


DROP:-
LANGUAGE DROP [TABLE | VIEW | INDEX | USER | ... ] [name]
[CASCADE | RESTRICT];
ALTER:-
ALTER [TABLE | VIEW | INDEX | USER | ... ] [name]
[... modifications ...];
TRUNCATE:-
TRUNCATE TABLE [table_name];
INSERT:-
INSERT INTO [table_name] ([column1], [column2], ...)
VALUES ([value1], [value2], ...);

UPDATE:-
DATA UPDATE [table_name]
MANIPULATIO SET [column1] = [new_value1], [column2] =
N LANGUAGE [new_value2], ...
WHERE [condition];

DELETE:-
DELETE FROM [table_name]
WHERE [condition];
SELECT:-
SELECT [column1], [column2], ...
FROM [table_name]
DATA QUERY WHERE [condition]
LANGUAGE [ORDER BY [column] [ASC/DESC]]
[LIMIT [number]];
1. COMMIT:
Syntax: COMMIT;
Function: Permanently saves changes made within a transaction
to the database.
2. ROLLBACK:
TRANSACTIO Syntax: ROLLBACK;

N CONTROL Function: Undoes all changes made within a transaction,


reverting the database to its previous state before the
LANGUAGE transaction started.
3. SAVEPOINT:
Syntax: SAVEPOINT [savepoint_name];
Function: Creates a temporary point within a transaction to which
you can rollback if needed. You can then use ROLLBACK TO
[savepoint_name] to revert to that specific point.
If you're referring to DCL within SQL, it's a subset of
commands used to manage user access and privileges within
the database. It doesn't have its own specific syntax but
utilizes keywords within SQL statements to grant, revoke, or
modify user permissions. Here are some examples:

DATA GRANT: GRANT SELECT, UPDATE ON users TO [username];


CONTROL GRANT [privilege_list] ON [object_type] [object_name]

LANGUAGE TO [grantee_list]

[WITH GRANT OPTION];

REVOKE: REVOKE DELETE ON orders FROM [username];


REVOKE [privilege_list] ON [object_type] [object_name]

FROM [grantee_list]

[CASCADE | RESTRICT];
These are constraints applied within a single table,
ensuring individual data values are valid and complete.
Common types include:
• Primary Key (PK): A unique identifier for each row
in a table, ensuring no duplicates.
ENTITY • Unique: Prevents duplicate values in a specific
column.
CONSTRAIN • NOT NULL: Disallows null values in a column.
TS • CHECK: Defines a logical expression for valid values
in a column.
• DEFAULT: Specifies a value automatically inserted
when a column is left blank.
• Primary Key (PK): • CHECK:
CREATE TABLE [table_name] ( CREATE TABLE [table_name] (
[column_name1] [data_type] PRIMARY KEY, [column_name1] [data_type] CHECK
([validation_expression]),
...
); ...
• Unique: );
CREATE TABLE [table_name] (
[column_name1] [data_type] UNIQUE, • DEFAULT:
...
CREATE TABLE [table_name] (
);
[column_name1] [data_type] DEFAULT [default_value],
• NOT NULL: ...
CREATE TABLE [table_name] ( );
[column_name1] [data_type] NOT NULL,
...
);
These connect two tables, ensuring data relationships
REFERENTIA are valid. The main type is:
• Foreign Key (FK): References a primary key in
L another table, guaranteeing data consistency
CONSTRAINT between tables. This prevents orphaned records (data
in one table without a corresponding record in the
S other).
•Foreign Key (FK):
ALTER TABLE [child_table_name]
ADD CONSTRAINT [constraint_name]
FOREIGN KEY ([child_column_name])
REFERENCES [parent_table_name] ([parent_column_name])
[ON DELETE [action]]
[ON UPDATE [action]];

ALTER TABLE: Modifies the existing child table.


ADD CONSTRAINT: Introduces a new constraint.
[constraint_name]: (Optional) Unique name for your reference.
FOREIGN KEY: Specifies the child table column referencing the
parent table.
REFERENCES: Indicates the parent table and its primary key
column.
ON DELETE/UPDATE: Define actions triggered by deletions/updates
in the parent table.
[action]: Options include CASCADE (propagate action to child
These are broader rules not directly defined in MySQL
but implied by the data's meaning and purpose. They
can't be explicitly enforced but are important for data
quality. Examples include:
SEMANTIC • Date consistency: Dates within a specific range or
maintaining chronological order.
CONSTRAIN • Values having specific relationships: E.g.,
TS product price always being greater than cost.
• Data adhering to real-world logic: E.g., employee
age always being positive.
1. Comments:-
CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(255),
price DECIMAL(10,2),
cost DECIMAL(10,2) -- Price should always be >= cost
);

2. Trigger Functions:-
CREATE TRIGGER price_check
BEFORE INSERT ON products
FOR EACH ROW
SET NEW.price = NULL
WHERE NEW.price < NEW.cost;

3. Check Constraint with subquery:-


CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(255),
price DECIMAL(10,2) CHECK (price >= (SELECT cost FROM products WHERE product_id =
NEW.product_id)),
cost DECIMAL(10,2)
);
COMPARISIO
N
Referential
Feature Entity Constraints Semantic Constraints
Constraints

Implicit, across
Scope Single table Multiple tables
database

Data validity and


Data relationships Real-world meaning
Focus completeness within
between tables and logic of data
a table

Exampl Primary key, Unique, Dates within range,


Foreign key
es NOT NULL price > cost, age > 0
INTERVIEW
QUESTIONS
Basic: Intermediate:
• What are the different types of • How can you enforce referential integrity
constraints in between two tables with different
MySQL? (Entity, Referential, Semantic) storage engines?
(InnoDB and MyISAM)
• What is the difference between a
primary key and a unique key? • What are the advantages and
disadvantages of using composite
• What is the purpose of a NOT NULL
primary keys?
constraint?
• Explain the difference between ON
• How do you define a CHECK constraint
DELETE CASCADE and ON UPDATE
on a column? CASCADE in a foreign key constraint.
• What is the impact of using a foreign key • How can you use constraints to handle
constraint? data validation within a single
table? (CHECK constraints, DEFAULT
values)
• What are some common mistakes
developers make when working with
INTERVIEW
QUESTIONS
Advanced: management problem.
• How can you use triggers to enforce Practice:
complex business rules beyond what
• Can you explain the difference between
constraints can do?
implicit and explicit constraints?
• Explain the concept of a CHECK
• What are some lesser-known constraint
constraint with a subquery.
types in MySQL? (e.g., GENERATE
• Discuss the performance implications of COLUMNS)
different types of constraints.
• How can you leverage constraints to
• How can you design a schema that improve the security of your database?
leverages constraints effectively for data
integrity and consistency?
• Describe a real-world scenario where
you used constraints to solve a data

You might also like