What are the basic SQL commands in MySQL?

0138

Basic SQL Commands in MySQL

MySQL (Structured Query Language) is a widely-used relational database management system (RDBMS) that allows users to create, manage, and manipulate databases. As a student, it's essential to familiarize yourself with the basic SQL commands in MySQL, as they form the foundation for working with databases.

Here are the most common and essential SQL commands in MySQL:

1. Data Definition Language (DDL) Commands

  1. CREATE: This command is used to create a new database, table, or other database objects.
    Example: CREATE DATABASE my_database;
    Example: CREATE TABLE users (id INT, name VARCHAR(50), email VARCHAR(50));

  2. ALTER: This command is used to modify the structure of an existing database object, such as a table.
    Example: ALTER TABLE users ADD COLUMN phone VARCHAR(20);

  3. DROP: This command is used to delete a database, table, or other database objects.
    Example: DROP TABLE users;

  4. TRUNCATE: This command is used to delete all the data from a table, but the table structure remains intact.
    Example: TRUNCATE TABLE users;

2. Data Manipulation Language (DML) Commands

  1. INSERT: This command is used to add new data to a table.
    Example: INSERT INTO users (id, name, email) VALUES (1, 'John Doe', '[email protected]');

  2. SELECT: This command is used to retrieve data from a table.
    Example: SELECT * FROM users;
    Example: SELECT name, email FROM users WHERE id = 1;

  3. UPDATE: This command is used to modify existing data in a table.
    Example: UPDATE users SET email = '[email protected]' WHERE id = 2;

  4. DELETE: This command is used to remove data from a table.
    Example: DELETE FROM users WHERE id = 3;

3. Data Control Language (DCL) Commands

  1. GRANT: This command is used to give user(s) access privileges to database objects.
    Example: GRANT SELECT, INSERT, UPDATE ON users TO 'myuser'@'localhost';

  2. REVOKE: This command is used to remove user access privileges to database objects.
    Example: REVOKE INSERT, UPDATE ON users FROM 'myuser'@'localhost';

4. Transaction Control Language (TCL) Commands

  1. START TRANSACTION: This command is used to begin a new transaction.
    Example: START TRANSACTION;

  2. COMMIT: This command is used to save the changes made during a transaction.
    Example: COMMIT;

  3. ROLLBACK: This command is used to undo the changes made during a transaction.
    Example: ROLLBACK;

Understanding these basic SQL commands in MySQL will provide a solid foundation for working with databases and help you manage data effectively. As you progress, you can explore more advanced SQL concepts and techniques to enhance your database management skills.

0 Comments

no data
Be the first to share your comment!