Skip to content

Latest commit

 

History

History
217 lines (125 loc) · 12.8 KB

notes.md

File metadata and controls

217 lines (125 loc) · 12.8 KB

DELETE

In MySQL, the DELETE command is used to delete one or more records from a table. The basic syntax for the DELETE command is as follows:

DELETE FROM table_name WHERE condition;

Here, table_name is the name of the table from which you want to delete records, and condition specifies the criteria for selecting the records to be deleted. If you omit the WHERE clause, all records in the table will be deleted.

For example, to delete all records from a table named mytable, you would use the following command:

DELETE FROM mytable; To delete only specific records based on a condition, you would use a command like this:

DELETE FROM mytable WHERE id = 1; This would delete only the record(s) where the id column has a value of 1.

UPDATE

In MySQL, the UPDATE command is used to modify existing records in a table. The basic syntax for the UPDATE command is as follows:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; Here, table_name is the name of the table you want to modify, column1, column2, etc. are the names of the columns you want to update, and value1, value2, etc. are the new values you want to set for those columns. The WHERE clause specifies the criteria for selecting the records to be updated.

For example, to change the name and age columns of a record where id equals 1 in a table named mytable, you would use the following command:

UPDATE mytable SET name = 'John', age = 30 WHERE id = 1; This would update the name column to "John" and the age column to 30 for the record(s) where the id column has a value of 1.

It is important to use a WHERE clause when using the UPDATE command, otherwise all records in the table will be updated.

ALTER

In MySQL, the ALTER command is used to modify the structure of a table, such as adding or removing columns, changing column data types, or renaming the table itself. The basic syntax for the ALTER command is as follows:

ALTER TABLE table_name action; Here, table_name is the name of the table you want to modify, and action specifies the modification you want to make.

Some examples of ALTER commands include:

  • Adding a new column to a table:

ALTER TABLE mytable ADD COLUMN new_column_name data_type; This would add a new column with the specified name and data type to the table named mytable.

  • Changing the data type of an existing column:

ALTER TABLE mytable MODIFY COLUMN column_name new_data_type; This would change the data type of the specified column in the table named mytable.

  • Renaming a table:

ALTER TABLE old_table_name RENAME TO new_table_name; This would rename the table named old_table_name to new_table_name.

Note that when using the ALTER command to modify a table, there is a possibility that data loss or corruption can occur. Therefore, it is important to backup your data before making any significant changes to a table's structure.

Difference between ALTER and UPDATE command

The main difference between ALTER and UPDATE commands in MySQL is that ALTER is used to modify the structure of a table, while UPDATE is used to modify the data within a table.

The ALTER command can be used to add or remove columns, change column data types, add or remove indexes, and rename a table. The ALTER command modifies the structure of the table and affects the way data is stored and retrieved from the table.

On the other hand, the UPDATE command is used to modify the values of existing records within a table. It changes the data stored in the table and does not affect the structure of the table.

In summary, ALTER is used to modify the structure of a table, while UPDATE is used to modify the data stored within the table.

ADD AFTER and ADD FIRST

In MySQL, when adding a new column to an existing table using the ALTER command, you can specify the position of the new column within the table using the "ADD COLUMN" statement with the "AFTER" or "FIRST" keyword.

ADD COLUMN ... AFTER: When using the "AFTER" keyword, you can specify the name of an existing column after which the new column will be added. For example:

ALTER TABLE mytable ADD COLUMN new_column_name data_type AFTER existing_column_name; This will add a new column with the specified name and data type after the existing column with the specified name.

ADD COLUMN ... FIRST: When using the "FIRST" keyword, the new column will be added as the first column in the table. For example:

ALTER TABLE mytable ADD COLUMN new_column_name data_type FIRST; This will add a new column with the specified name and data type as the first column in the table.

Note that the "AFTER" and "FIRST" keywords are optional when using the "ADD COLUMN" statement in the ALTER command. If you do not specify either keyword, the new column will be added as the last column in the table.

DROP

In MySQL, the DROP command is used to delete a database, a table, or an index. The basic syntax for the DROP command is as follows:

  • To drop a database: DROP DATABASE database_name;

  • To drop a table: DROP TABLE table_name;

  • To drop an index: DROP INDEX index_name ON table_name;

Here, database_name is the name of the database you want to delete, table_name is the name of the table you want to delete, and index_name is the name of the index you want to delete.

It is important to note that the DROP command permanently deletes the database, table, or index, along with all data it contains. Therefore, it is important to use the DROP command with caution and make sure you have a backup of any data you want to preserve.

DROP command with ALTER command

The DROP command and the ALTER command are both used for modifying the structure of a table, but they have different purposes. The DROP command is used to delete an existing table, database or index, while the ALTER command is used to modify the structure of a table.

It is possible to use the DROP command within an ALTER command to delete a column from a table. The syntax for using the DROP command with ALTER is as follows:

ALTER TABLE table_name DROP COLUMN column_name; Here, table_name is the name of the table from which you want to delete a column, and column_name is the name of the column you want to delete.

This ALTER command will remove the specified column from the table permanently, along with all data stored in that column. Therefore, it is important to make sure you have a backup of any data you want to preserve before using the DROP command within an ALTER command.

DISTINCT

In MySQL, the DISTINCT keyword is used in a SELECT statement to eliminate duplicate rows from the result set. It is used in conjunction with the SELECT keyword to retrieve unique values from one or more columns of a table.

The basic syntax for using the DISTINCT keyword in MySQL is as follows:

SELECT DISTINCT column1, column2, ... FROM table_name;

Here, column1, column2, etc. are the columns from which you want to retrieve distinct values, and table_name is the name of the table containing those columns.

For example, if you have a table named "customers" with columns "customer_id", "first_name", and "last_name", and you want to retrieve the unique values from the "last_name" column, you would use the following query:

SELECT DISTINCT last_name FROM customers;

This query would return a list of unique last names from the "customers" table, with no duplicates.

Note that the DISTINCT keyword only applies to the columns listed in the SELECT statement. If you specify multiple columns, the result set will contain unique combinations of values from those columns, but not necessarily unique values for each individual column.

COUNT

In MySQL, the COUNT function is used to count the number of rows that match a specified condition in a table. It can be used with the SELECT statement to retrieve the number of rows in a table or the number of rows that match a specific condition.

The basic syntax for using the COUNT function is as follows:

SELECT COUNT(*) FROM table_name; This query will return the total number of rows in the table.

You can also use the COUNT function with a WHERE clause to count the number of rows that match a specific condition. For example, if you have a table named "customers" with columns "customer_id", "first_name", and "last_name", and you want to count the number of customers whose last name is "Smith", you would use the following query:

SELECT COUNT(*) FROM customers WHERE last_name = 'Smith'; This query will return the number of rows in the "customers" table where the last name is "Smith".

Note that the COUNT function does not count NULL values unless you use the COUNT(*) syntax. In addition, you can use the COUNT function with other functions such as AVG, SUM, and MAX to perform more complex calculations on the data.

SUM

In MySQL, the SUM function is used to calculate the sum of values in a specific column of a table. It is often used in combination with the SELECT statement to retrieve the total value of a specific column.

The basic syntax for using the SUM function is as follows:

SELECT SUM(column_name) FROM table_name; Here, column_name is the name of the column for which you want to calculate the sum, and table_name is the name of the table containing that column.

For example, if you have a table named "sales" with columns "product_name" and "sales_amount", and you want to calculate the total sales amount for all products, you would use the following query:

SELECT SUM(sales_amount) FROM sales; This query will return the total sales amount for all products in the "sales" table.

Note that the SUM function only works on numerical data types such as INT, FLOAT, and DECIMAL. It cannot be used with non-numeric data types such as VARCHAR or TEXT.

AVG

In MySQL, the AVG function is used to calculate the average value of a specific column in a table. It is often used in combination with the SELECT statement to retrieve the average value of a specific column.

The basic syntax for using the AVG function is as follows:

SELECT AVG(column_name) FROM table_name; Here, column_name is the name of the column for which you want to calculate the average, and table_name is the name of the table containing that column.

For example, if you have a table named "students" with columns "student_id", "name", and "score", and you want to calculate the average score for all students, you would use the following query:

SELECT AVG(score) FROM students; This query will return the average score for all students in the "students" table.

Note that the AVG function only works on numerical data types such as INT, FLOAT, and DECIMAL. It cannot be used with non-numeric data types such as VARCHAR or TEXT.

GROUP BY

In MySQL, the GROUP BY clause is used in a SELECT statement to group the result set based on one or more columns. It is often used in combination with aggregate functions such as SUM, COUNT, AVG, MIN, and MAX to perform calculations on groups of data.

The basic syntax for using the GROUP BY clause is as follows:

SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name; Here, column_name is the name of the column you want to group by, and aggregate_function is the function you want to use to perform calculations on the grouped data.

For example, if you have a table named "sales" with columns "product_name", "sales_amount", and "sales_date", and you want to calculate the total sales amount for each product, you would use the following query:

SELECT product_name, SUM(sales_amount) FROM sales GROUP BY product_name; This query will return the total sales amount for each product in the "sales" table.

Note that when using the GROUP BY clause, the result set will contain one row for each group, rather than one row for each record in the table. Also, any columns that are not included in the GROUP BY clause must be included in an aggregate function.

HAVING

In MySQL, the HAVING clause is used in a SELECT statement to filter the result set based on aggregate function values. It is often used in combination with the GROUP BY clause to filter groups of data based on specific criteria.

The basic syntax for using the HAVING clause is as follows:

SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name HAVING condition; Here, condition is the filtering condition that the result set must meet in order to be included in the query result.

For example, if you have a table named "sales" with columns "product_name", "sales_amount", and "sales_date", and you want to find the products that have sold more than $10,000, you would use the following query:

SELECT product_name, SUM(sales_amount) FROM sales GROUP BY product_name HAVING SUM(sales_amount) > 10000; This query will return the product names and their total sales amounts for products that have sold more than $10,000.

Note that the HAVING clause is applied after the GROUP BY clause, so it can only be used with aggregate functions such as SUM, COUNT, AVG, MIN, and MAX. Also, any columns that are not included in the GROUP BY clause must be included in an aggregate function or in the HAVING clause.