You are on page 1of 3

MySQL Commands

1.The following syntax can create a database.


-create database db_name;
2.Use the following syntax to replace the current database with another
database you’re working on:
-USE db_name;
3. Using the following syntax, we can permanently destroy a database and its
associated files. All the tables that are a part of that database will be deleted if
we use the following command:-
-drop database db_name;
4. Use the following syntax to display all databases on the current server:
SHOW DATABASES;
5. This command displays all tables in a current database.
SHOW TABLES;
6. This command creates a new table:
This command creates a new table:
create table table_name
(Column_name1 datatype, Column_name2 datatype……);
The datatype can be integer, char (fixed length sring), varchar (variable length
string), binary etc. The data which is going to be stored in a column decides
the datatype of the column.
7. To insert values into a table type the following command:
INSERT INTO table_name
VALUES (value1, value2, value3, …);
For example, to insert first column of the students table, you have to type the
following command:
INSERT INTO Marks
VALUES (001, ‘Ashish’,94);
8. In a table, add a new column:
ALTER TABLE table
ADD column_name datatype;
For example to add Mentor’s Name column, you have to type the following
command:
ALTER TABLE Marks
ADD Mentor varchar(100);
9. To remove a column from a table, do the following:
ALTER TABLE table_name
DROP column_name;
10. To add a primary key to a table, do the following;
ALTER TABLE table_name
ADD PRIMARY KEY (column_name,…);
11. To remove the primary key of a table, do the following:
ALTER TABLE table_name
DROP PRIMARY KEY;
12. You can drop a table using the following command:
DROP TABLE [IF EXISTS] table_name;
13. To show the columns of a table, type the following code:
DESCRIBE table_name;
14. To query the entire contents of a table, use the following command:
SELECT * FROM table_name;
15. If you want MySQL to show only some columns of the table, you can use
the following syntax:
SELECT
column1, column2, …
FROM
Table_name;

16. 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;
For example, if we want to write a query to retrieve the name of students
whose score is less than 80, it will look something like this:
SELECT Name from Marks
WHERE Marks <80;
17. Counting the number of rows in a table:
SELECT COUNT(*)
FROM table_name;
18. To add a new row to a table, do the following:
INSERT INTO table_name(column_list)
VALUES(value_list);
19. Update data for a set of rows defined by a WHERE clause criteria:
UPDATE table_name
SET column_1 = value_1,

WHERE condition;
20. To delete all rows in a table, the syntax is:
DELETE FROM table_name;
21. To delete rows specified by a condition:
DELETE FROM table_name
WHERE condition;

You might also like