MYSQL BASIC COMMANDS
MYSQL BASIC COMMANDS
1. Create Database
Syntax:
SHOW DATABASES;
SHOW DATABASES;
USE database_name;
USE mydatabase;
Description: This command lists all the tables in the currently selected database.
Example:
USE mydatabase;
SHOW TABLES;
Output:
+----------------+
| Tables_in_mydatabase |
+----------------+
| users |
| orders |
| products |
+----------------+
Attributes:
column1, column2, ...: Names of the columns.
datatype: Data type for the column (e.g., INT, VARCHAR, DATE).
constraint: Constraints for the column (e.g., PRIMARY KEY, NOT NULL, UNIQUE).
COMMENT: Optional comment for the column.
Example: Creating a students table with various data types and constraints:
Example:
Output:
Possible Conditions:
1. Single Table Rename:
Output:
This command will rename the students table to learners and the courses table to subjects in a
single operation.
Rename Table Using sp_rename in SQL Server
Description: The sp_rename stored procedure is used to rename user-created objects in the current
database, such as tables, columns, indexes, and other objects.
Syntax:
Output:
Caution: Changing any part of an object name could break scripts and stored procedures.
This message indicates that renaming an object might affect scripts and stored procedures that
reference the old name.
Example: Renaming a Column
Output:
Caution: Changing any part of an object name could break scripts and stored procedures.
Possible Conditions:
1. Renaming a Table:
Renames a table.
3. Renaming a Column:
Example:
Output:
This command will completely remove the students table from the database.
Delete Table Records in MySQL
Description: The DELETE statement is used to delete specific records from a table based on a
condition.
Syntax:
Example:
Output:
This command will delete the record from the students table where the student_id is 1.
Example with Sample Values
Let's create a students table, insert some sample values, and then demonstrate deleting records and
the table itself.
Create Table:
Output Table:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
Updated Table:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
Output:
This will remove the students table and all its data from the database.
Creating a New Table from Existing Data Types in MySQL
Description: The CREATE TABLE ... AS SELECT statement is used to create a new table based on the
structure and data of an existing table. This can be done in several ways, depending on whether you
want to copy the structure only, the structure and data, or specific columns.
Syntax:
Example:
Output:
This command creates a new table new_students with the same structure and data as
the students table.
Possible Ways to Create a New Table from Existing Data Types
1. Copying Structure and Data:
Copies both the structure and data from the existing table.
3. Copying Structure Only:
CREATE TABLE new_students AS SELECT * FROM students WHERE birth_date > '2000-
01-01';
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
This example demonstrates how to create a new table from an existing table's structure and data.
INSERT INTO in MySQL
Description: The INSERT INTO statement is used to add new records to a table. It can insert data into
all columns or specific columns of a table.
Syntax and Examples
1. Inserting into All Columns: Syntax:
Example:
4. INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example:
6. INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...),
(value1, value2, ...), ...;
Example:
8. INSERT INTO table_name (column1, column2, ...) SELECT column1, column2, ... FROM
another_table WHERE condition;
Example:
Output Table:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
+------------+------------+-----------+------------+----------------------+
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
| 4 | Bob | Brown | 2002-03-10 | [email protected] |
+------------+------------+-----------+------------+----------------------+
These examples demonstrate various ways to use the INSERT INTO statement to add data to
the students table.
Update Records in MySQL
Description: The UPDATE statement is used to modify existing records in a table. You can update one
or multiple columns and rows based on a specified condition.
Syntax and Examples
1. Updating Specific Columns: Syntax:
2. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:
4. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:
Example:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
Output:
Updated Table:
+------------+------------+-----------+------------+-------------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+-------------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+-------------------------+
Updating Multiple Columns:
Output:
Updated Table:
+------------+------------+-----------+------------+-------------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+-------------------------+
| 1 | Jonathan | Doe | 2000-01-01 | [email protected]|
| 2 | Jane | Smith | 1999-05-15 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+-------------------------+
Output:
Updated Table:
+------------+------------+-----------+------------+-------------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+-------------------------+
| 1 | Jonathan | Doe | 2000-01-01 | [email protected]|
| 2 | Jane | Smith | 1999-05-15 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+-------------------------+
Output:
+------------+------------+-----------+------------+-------------------------+-----------
+
| student_id | first_name | last_name | birth_date | email | is_active
|
+------------+------------+-----------+------------+-------------------------+-----------
+
| 1 | Jonathan | Doe | 2000-01-01 | [email protected]| 0
|
| 2 | Jane | Smith | 1999-05-15 | [email protected] | 0
|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]| 0
|
+------------+------------+-----------+------------+-------------------------+-----------
+
These examples demonstrate various ways to use the UPDATE statement to modify data in
the students table.
Alter Table in MySQL
Description: The ALTER TABLE statement is used to modify the structure of an existing table. This can
include adding, deleting, or modifying columns, as well as adding or dropping constraints.
Syntax and Examples
1. Adding a Column: Syntax:
Example:
Example:
Example:
Example:
Example:
Example:
Example:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
Adding a Column:
Output:
+------------+------------+-----------+------------+----------------------+------+
| student_id | first_name | last_name | birth_date | email | age |
+------------+------------+-----------+------------+----------------------+------+
| 1 | John | Doe | 2000-01-01 | [email protected] | NULL |
| 2 | Jane | Smith | 1999-05-15 | [email protected]| NULL |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]| NULL |
+------------+------------+-----------+------------+----------------------+------+
Dropping a Column:
Output:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
Modifying a Column:
Renaming a Column:
Output:
+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | dob | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+
Output:
Adding a Constraint:
Output:
Dropping a Constraint:
Output:
DESCRIBE table_name;
Example:
DESCRIBE students;
Output:
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| student_id | int | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | YES | | NULL | |
| last_name | varchar(50) | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| email | varchar(100)| YES | UNI | NULL | |
+-------------+-------------+------+-----+---------+----------------+
Example:
Output:
RowGuidCol
----------
No rowguidcol column defined.
Data_located_on_filegroup
-------------------------
PRIMARY
This output provides comprehensive details about the students table, including column attributes,
constraints, and other metadata.