mysql
mysql
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least
2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at
least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"
#=================================================
INSERT INTO Student VALUES(1, "Raj Kumar", "M", 93, "20001117", 9586774748,
"Science"), (3, "Ankit Sharma", "M", 76, "20000202",8567490078, "Science"), (4,
"Radhika Gupta", "F", 78, "19991203", 9818675444, "Humanities"), (5, "Payal Goel",
"F", 82, "19980421", 9845639990, "Vocational"), (6, "Diksha Sharma", "F", 80,
"19991217", 9897666650, "Humanities"), (7, "Gurpreet Kaur", "F", 65, "20000104",
7560567890, "Science"), (9, "Shreya Anand", "F", 70, "19991008", 8876543988,
"Vocational"), (10, "Prateek Mittal", "M", 75, "20001225", 9999967543, "Science");
#==========================================================
UPDATE table_name SET column1 = value1;
UPDATE table_name SET column_1 = value_1, WHERE condition
SHOW DATABASES;
SHOW TABLES;
DESCRIBE table_name;
CREATE TABLE table_name(Column_name1 datatype, Column_name2 datatype……);
INSERT INTO table_name VALUES (value1, value2, value3, …);
To add a new column=>
ALTER TABLE table ADD column_name datatype;
To remove a column=>
ALTER TABLE table_name DROP column_name;
To add an index to a table on a column with a specified name=>
ALTER TABLE table ADD INDEX [name](column, …);
To add a primary key to a table
ALTER TABLE table_name ADD PRIMARY KEY (column_name,…);
To remove the primary key of a table
ALTER TABLE table_name DROP PRIMARY KEY;
To drop a table
DROP TABLE table_name;
To drop a database
DROP DATABASE database_name;
To rename a table
RENAME TABLE old_table_name TO new_table_name;
To rename a database
RENAME DATABASE old_database_name TO new_database_name;
To rename a column
ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
To modify the datatype of a column
ALTER TABLE table_name MODIFY column_name datatype;
To add a column after another column
ALTER TABLE table_name ADD column_name datatype AFTER column_name;
To add a column before another column
ALTER TABLE table_name ADD column_name datatype BEFORE column_name;
To add a column at the beginning of the table
ALTER TABLE table_name ADD column_name datatype FIRST;
To add a column at the end of the table
ALTER TABLE table_name ADD column_name datatype LAST;
To delete all the records from a table
DELETE FROM table_name;
To delete all the records from a table and reset the auto increment value
TRUNCATE TABLE table_name;
To delete a record from a table
DELETE FROM table_name WHERE condition;
To show the columns of a table
SHOW COLUMNS FROM table_name;
To show the indexes of a table
SHOW INDEX FROM table_name;
To show the status of a table
SHOW TABLE STATUS FROM database_name LIKE 'table_name';
To show the status of a database
SHOW DATABASE STATUS FROM database_name LIKE 'table_name';
To create an index with the specified name on a table:
CREATE INDEX index_name ON table_name (column,…);
TO create a unique index with the specified name on a table:
CREATE UNIQUE INDEX index_name ON table_name (column,…);
To drop an index from a table:
DROP INDEX index_name ON table_name;
#==========================================================
We can filter records using WHERE clause. It returns only those records which
fulfill a specified set of condition.
SELECT column_list
FROM table_name
WHERE condition;
You can change the output of the column name using column alias:
SELECT
column1 AS alias_name,
expression AS alias,
...
FROM
Table_name;
SELECT
select_list
FROM
table_name
ORDER BY
column1 ASC [DESC],
column2 ASC [DESC];
To query data from multiple tables using inner join, use the following command:
SELECT select_list
FROM table1
INNER JOIN table2 ON condition;
SELECT select_list
FROM table1
LEFT JOIN table2 ON condition;
SELECT select_list
FROM table1
RIGHT JOIN table2 ON condition;
SELECT select_list
FROM table1
CROSS JOIN table2;