DEV Community

Zaenal Arifin
Zaenal Arifin

Posted on

MySQL Database Management


MySQL Database Management

Creating a Database


To create a new database in MySQL, you can use the following SQL command:


CREATE DATABASE your_database_name;
Enter fullscreen mode Exit fullscreen mode

Using a Database


After creating a database, you can use it by executing the following SQL command:


USE your_database_name;
Enter fullscreen mode Exit fullscreen mode

Creating a Table


To create a table in a MySQL database, you can use the following SQL syntax:


CREATE TABLE table_name (
      column1 datatype1 constraints,
      column2 datatype2 constraints,
      ...
    ); 
Enter fullscreen mode Exit fullscreen mode

Example:


CREATE TABLE users (
      id INT AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(50),
      email VARCHAR(100)
    ); 
Enter fullscreen mode Exit fullscreen mode

Inserting Data


To insert data into a table, you can use the following SQL syntax:


INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); 
Enter fullscreen mode Exit fullscreen mode

Example:


INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com'); 
Enter fullscreen mode Exit fullscreen mode

Updating Data


To update data in a table, you can use the following SQL syntax:


UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; 
Enter fullscreen mode Exit fullscreen mode

Example:


UPDATE users SET email = 'newemail@example.com' WHERE id = 1; 
Enter fullscreen mode Exit fullscreen mode

Deleting Data


To delete data from a table, you can use the following SQL syntax:


DELETE FROM table_name WHERE condition;
Enter fullscreen mode Exit fullscreen mode

Example:


DELETE FROM users WHERE id = 1; 
Enter fullscreen mode Exit fullscreen mode

Dropping a Table


To drop (delete) a table from the database, you can use the following SQL syntax:


DROP TABLE table_name; 
Enter fullscreen mode Exit fullscreen mode

Example:


DROP TABLE users; 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)