0% found this document useful (0 votes)
488 views2 pages

Ansi SQL Using Mysql

The document outlines the key components of ANSI SQL using MySQL, including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). It also covers constraints, SQL operators, functions, clauses, joins, sub-queries, views, and indexes. Each section provides essential commands and concepts necessary for managing and manipulating databases effectively.

Uploaded by

black
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
488 views2 pages

Ansi SQL Using Mysql

The document outlines the key components of ANSI SQL using MySQL, including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). It also covers constraints, SQL operators, functions, clauses, joins, sub-queries, views, and indexes. Each section provides essential commands and concepts necessary for managing and manipulating databases effectively.

Uploaded by

black
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Query - Database / ANSI SQL using MySQL

1. DDL (Data Definition Language)


- CREATE: Used to create tables, databases, indexes, and other objects.
- ALTER: Used to modify existing database structures.
- DROP: Deletes tables, databases, indexes, etc.
- TRUNCATE: Removes all records from a table without logging individual row
deletions.
- RENAME: Changes the name of an existing database object.

2. DML (Data Manipulation Language)


- SELECT: Retrieves data from a database.
- INSERT: Adds new records to a table.
- UPDATE: Modifies existing records in a table.
- DELETE: Removes records from a table.

3. DCL (Data Control Language)


- GRANT: Provides specific privileges to users.
- REVOKE: Removes privileges from users.

4. TCL (Transaction Control Language)


- COMMIT: Saves all changes made by the transaction.
- ROLLBACK: Reverts changes made by the transaction.
- SAVEPOINT: Creates a point in a transaction to which you can later roll back.

5. Understanding Constraints and Their Types


- NOT NULL: Ensures a column cannot have a NULL value.
- UNIQUE: Ensures all values in a column are unique.
- PRIMARY KEY: Uniquely identifies each record in a table.
- FOREIGN KEY: Ensures referential integrity between tables.
- CHECK: Ensures that all values in a column satisfy a condition.
- DEFAULT: Sets a default value for a column if no value is provided.

6. SQL Operators
- Arithmetic Operators: +, -, *, /, %
- Comparison Operators: =, !=, <>, >, <, >=, <=
- Logical Operators: AND, OR, NOT
- Bitwise Operators: &, |, ^, ~, <<, >>

7. SQL Functions
- Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX()
- String Functions: CONCAT(), LENGTH(), UPPER(), LOWER(), SUBSTRING()
- Date Functions: NOW(), CURDATE(), DATE_FORMAT()
- Numeric Functions: ROUND(), CEIL(), FLOOR(), ABS()
- Conversion Functions: CAST(), CONVERT()

8. Clauses in SQL
- WHERE: Filters records based on conditions.
- GROUP BY: Groups records with identical values.
- HAVING: Filters groups based on conditions.
- ORDER BY: Sorts the result set in ascending or descending order.
- LIMIT: Restricts the number of records returned.

9. Joins and Their Types


- INNER JOIN: Returns matching rows from both tables.
- LEFT JOIN: Returns all rows from the left table and matching rows from the
right table.
- RIGHT JOIN: Returns all rows from the right table and matching rows from the
left table.
- FULL OUTER JOIN: Returns all rows when there is a match in either table.
- SELF JOIN: A table joins itself.
- CROSS JOIN: Returns the Cartesian product of both tables.

10. Sub-Queries
- A query nested inside another query.
- Used with SELECT, INSERT, UPDATE, DELETE.
- Can return single or multiple values.

11. Views and Indexes


- View: A virtual table based on a query result.
- Created using CREATE VIEW statement.
- Index: Improves database performance by allowing faster data retrieval.
- Created using CREATE INDEX statement.

Common questions

Powered by AI

SQL Logical Operators—AND, OR, NOT—are used to filter data within a query by combining multiple conditions . AND is used when all conditions must be met, best for narrowing down results (e.g., retrieving records where both 'status' is active AND 'quantity' is greater than 10). OR is used when any condition can be met (e.g., retrieving records where either 'status' is active OR 'priority' is high). NOT negates a condition, used to exclude specific records (e.g., retrieving records where 'status' is NOT inactive).

SQL conversion functions CAST() and CONVERT() are used to change a data type of a value to another data type, facilitating operations across different data types and ensuring data compatibility . They are particularly useful in expressions involving date, time, and numeric data transformations where format compatibility is crucial. Implications include potential precision loss or format errors if not managed properly, such as converting floating-point to integer data types, which may truncate decimal portions and affect calculation results.

ORDER BY clause sorts the result set in either ascending or descending order, boosting usability by presenting data in a more meaningful order, which is essential for reports and analysis where order matters . LIMIT clause restricts the number of records returned by a query, enhancing performance by reducing the dataset size that needs to be processed and transferred, which is crucial for large datasets to minimize system load and response times . These clauses work together to provide more efficient and maintainable data output.

SQL aggregate functions such as COUNT(), SUM(), AVG(), etc., are often used with GROUP BY and HAVING clauses to generate grouped summaries and apply conditions on these groups . For example, in a sales database, you could use SUM() to calculate total sales per product category, GROUP BY to organize the data by category, and HAVING to filter out categories with total sales below a certain threshold. This approach enables more efficient processing and insightful analysis of categorized data sets.

SQL arithmetic operators (+, -, *, /, %) perform basic mathematical calculations on numeric data types within queries . Considerations include ensuring data type compatibility, as operations on incompatible types (like adding a string to a number) can cause errors . Precision and overflow are potential pitfalls, particularly in integer division which truncates decimals, and operations exceeding data type limits leading to overflow errors . Careful planning ensures accurate mathematical results in SQL operations.

A PRIMARY KEY constraint ensures data integrity by uniquely identifying each record in a table; it is a combination of NOT NULL and UNIQUE, preventing duplicate and null values for the key attribute . A FOREIGN KEY constraint references the PRIMARY KEY of another table and maintains referential integrity by ensuring that the values in the foreign key column in the child table match a primary key in the parent table . This interaction prevents data inconsistencies, such as orphaned records, which might occur if a child record references a non-existent parent.

A SELF JOIN is used when comparing rows within the same table; it provides a way to relate rows to others in the same data set . It's particularly useful in scenarios such as hierarchical structures or finding relationships like an employee-manager hierarchy within a single employee table, where each employee has a manager ID that corresponds to another employee's ID . This allows the query to cross-reference the same table to draw meaningful connections between its records.

Indexes improve database performance by enabling faster data retrieval through quicker access to rows and reduced disk I/O operations . They function as pointers to data locations within a table. However, the drawbacks include additional storage space requirements and slower performance on data modification operations like INSERT, UPDATE, or DELETE, as the index must be updated consistently . Effective index strategies should balance query speed improvements with increased system resource usage.

FULL OUTER JOIN is used when you want to retrieve all records when there is a match in either table, including unmatched rows from both tables . It is useful in scenarios where data from both tables needs to be combined comprehensively, such as combining user details from two different departments. INNER JOIN, conversely, returns only the matching rows from both tables, excluding any unmatched records , and is suited for scenarios where only closely related data is required, such as listing customers with corresponding orders.

DELETE removes records from a table while logging individual row deletions, and each deletion can be rolled back using ROLLBACK in transaction control, as it is a part of DML and logs each operation . TRUNCATE, however, removes all records from a table without logging individual deletions, operates faster as it deallocates pages, and cannot be rolled back unless explicitly wrapped in a transaction . TRUNCATE is part of DDL because it affects the definition of the table, not just data within it .

You might also like