MY SQL is a programming language used to manage and manipulate databases. If you'd like to know more about SQL, I can help with topics like:
*SQL Basics*: How to create tables, insert data, update records, and query databases.
*Advanced SQL*: Topics like JOIN operations, subqueries, stored procedures, and triggers.
*Database Design*: Normalization, database relationships, and indexing.
let's see step by step for the first step
Create Data Base
[ CREATE DATABASE UMESH ;]
To view database
[mysql> Show Database;]
To delete Database
[Drop Database Umesh;]
Essential Mysql Commands
Select:- Choose a specific data from your Database.
Update:- Update Data in your Database.
Delete:- Delete data from your Database.
Insert into:- insert new data into the database.
Create Database:- Generate a new Database.
Alter Database:- modify the existing database.
Create Table:- Create a new table in a database.
Alter table:- change the Selected table.
Drop-Table:- Delete a Table.
Create Index:- Create an index
Delete Index:- delete an index
Working with Tables
Create a New Simple Table
View Tables
Delete a Table
Add an Additional Selection Clause
Include an additional pointer that indicates what type of data you need.SELECT * FROM movies WHERE budget='1';
SELECT * FROM movies WHERE year='2020' AND rating='9';
Delete a Row
Use SELECT FROM syntax and WHERE clause to specify what rows to delete.
DELETE FROM movies WHERE budget='1';
Update Rows
Similarly, you can use different clauses to update all or specified rows in your table. To update all rows:UPDATE table_name
SET column1 = value1,...;
To update data only in a specified set of rows you can use the WHERE clause:
UPDATE table_name
SET column_1 = value_1,
WHERE budget='5'
You can also update, select or delete rows using the JOIN clause. It comes particularly handy when you need to manipulate data from multiple tables in a single query.
Here's how to update rows with JOIN:
UPDATE table_name
INNER JOIN table1 ON table1.column1 = table2.column2
SET column1 = value1
WHERE budget='5'
Edit a Column
Sort Entries in a Column
You can sort the data in all columns and rows the same way you do in Excel e.g. alphabetically or from ascending to descending value.
SELECT * FROM users ORDER BY last_name ASC; SELECT * FROM users ORDER BY last_name DESC;
Search Columns
Here's how you can quickly find the information you need using WHERE and LIKE syntax:SELECT * FROM movies WHERE genre LIKE 'com%'; SELECT * FROM movies WHERE title LIKE '%';
You can also exclude certain items from the search with NOT LIKE:
SELECT * FROM movies WHERE genre NOT LIKE 'hor%';
Select a Range
Or you can bring up a certain data range using the next command:SELECT * FROM movies WHERE rating BETWEEN 8 AND 10;
Concentrate Columns
You can mash up two or more columns together with the CONCAT function:SELECT CONCAT(first_name, '', last_name) AS 'Name', dept FROM users;
Data Types
Working With Indexes
How to Create an Index
How to Delete an Index in MySQL
Working with Views
Working With Triggers
A trigger is a database object, associated with a table. It activates whenever a specific event happens for the table.For example, you can set up triggers for events such as:
·
- Row or delete updates
- Row information inserts
How to Create a Trigger
To create a simple trigger that will pop up before or after a certain operation such as INSERT, UPDATE or DELETE, use this code:{BEFORE
AFTER} {INSERT | UPDATE DELETE }
ON table_name FOR EACH ROW
trigger_body;
Review All Triggers in Your Database
Search your database for all the active triggers using LIKE and WHERE clauses.SHOW TRIGGERS
[{FROM IN} database_name]
[LIKE 'pattern' | WHERE search_condition];
How to Delete a Trigger
To remove a trigger, use the DROP command:DROP TRIGGER [IF EXISTS] trigger_name;
Stored Procedures for MySQL
Stored procedures are reusable SQL code snippets that you can store in your database and use- as-needed over and over again. They save you tons of time since you don't need to write a query from scratch. Instead, you just call it to execute it.How to Create a Stored Procedure in MySQL
Here's how to create a simple stored procedure with no additional parameters:CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
And here's another stored procedure example featuring the WHERE clause:
CREATE PROCEDURE SelectAllMovies @Title varchar(30) AS
SELECT * FROM Movies WHERE Title = @Title
GO;
Review All Stored Procedures
Similarly to triggers, you can review all stored procedures with LIKE and WHERE:SHOW PROCEDURE STATUS
[LIKE 'pattern' | WHERE search_condition];
How to Delete a Stored Procedure
To get rid of a stored procedure you no longer need, use DROP:DROP PROCEDURE [IF EXISTS] procedure name;
Logical Operators
Logical operators enable you to add more than one condition in WHERE clause. This makes them super handy for more advanced search, update, insert and delete queries.In MySQL you have three main logical operators:
- AND - use it to filter records that rely on 1+ condition. This way you can call records that satisfy all the conditions separated by AND.
- OR - call records that meet any of the conditions separated by OR.
- NOT review records that do not meet a certain condition (e.g. NOT blue). It's a handy operator from excluding certain data.
- Plus, some additional special operators:
- BETWEEN - select or search data between a range of set min and max values.
- LIKE - compare one record to another. Handy operator for search.
- IS NULL - compare some value with a NULL value.
- In - determine if a value or expression matches one of the values on your list.
- ALL - compare a value or expression to all other values in a list.
- ANY - compare a value or expression to any value in your list according to the specified condition.
- EXISTS test if a certain record exists.
Aggregate Functions
Aggregate functions in MySQL allow you to run a calculation on a set of values and return a single scalar value. In essence, they are a great way to find the needed data faster and organize it betterusing GROUP BY and HAVING clauses of the SELECT statement.
Below is an overview of these:
Find the smallest value of the selected column in your table:
SELECT MIN (column_name)
FROM table_name
WHERE condition;
Does the opposite and returns the largest value of the selected column:
SELECT MAX (column_name)
FROM table_name WHERE condition;
Call up several rows that meet the specified criteria:
SELECT COUNT (column_name)
FROM table_name
WHERE condition;
Get the average value of a numeric column that you selected:
SELECT AVG (column_name)
FROM table_name
WHERE condition;
Arithmetic, Bitwise, Comparison, and
Compound Operators
Arithmetic Operators +=*===Bitwise Operators &, 1, ^
Comparison Operators = =, <, >, <=, >=, <>
Compound Operators /=,=, &=, | *=
SQL Database Backup Commands
Finally, don't forget to regularly back up your progress as you are testing different commands and code snippets.
MySQL dump -u Username -p dbName YouWant > databasename_backup.sql
MySQL u Username -p dbNameYouWant < databasename_backup.sql
* If you found this article useful, share it.*
MS SQL Server interview questions for developers ~ SoftCodeLearner
No comments:
Post a Comment