2. Creating Database
To create a new database using MySQL, you can
use the CREATE DATABASE statement. Here's the
basic syntax for creating a database:
CREATE DATABASE database_name;
• database_name: This is the name you want to
give to your new database.
3. Creating a Table
To create a table in a MySQL database, you can use the CREATE
TABLE statement. This statement allows you to define the table's
structure, including its columns and their data types. Here's the basic
syntax for creating a table:
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
4. • table_name: This is the name you want to give to your new table.
• column1, column2, ...: These are the names of the columns in the
table.
• datatype: Specifies the data type for each column (e.g., INT,
VARCHAR, DATE, etc.).
• constraints: Optional constraints, such as PRIMARY KEY, NOT
NULL, UNIQUE, AUTO_INCREMENT, and more, that define rules
for each column.
5. CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
birthdate DATE
);
In this example, we create a table named "users" with columns for
user ID, username, email, and birthdate. We've specified some
common constraints like PRIMARY KEY, NOT NULL, and UNIQUE to
enforce data integrity rules.
6. Inserting Rows into a Table
To insert rows (records) into a table in a MySQL database, you can use
the INSERT INTO statement. This statement allows you to specify the
values you want to insert into the columns of the table. Here's the basic
syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
7. • table_name: This is the name of the table into which you want to
insert data.
• (column1, column2, column3, ...): This is an optional list of column
names where you want to insert data. If you omit this part, you
should provide values for all columns in the same order they are
defined in the table.
• VALUES (value1, value2, value3, ...): This is where you specify the
values you want to insert into the corresponding columns. The
number of values should match the number of columns you're
inserting into.
8. INSERT INTO users (username, email, birthdate)
VALUES ('john_doe', 'john@example.com', '1990-05-15’);
In this example, we're inserting data into the "users" table, specifying
values for the "username," "email," and "birthdate" columns.
9. Updating and Deleting Data into a Table
In MySQL, you can use the UPDATE and DELETE statements to
modify or remove data from a table. Here's how to use each of these
statements:
10. Updating Data with the UPDATE Statement:
The UPDATE statement is used to modify existing records in a table.
You specify which rows to update and the new values for the columns.
The basic syntax is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
WHERE condition: This is an optional condition that specifies which
rows to update. If omitted, all rows in the table will be updated.
11. UPDATE users
SET email = 'new_email@example.com'
WHERE username = 'john_doe’;
This query updates the email address for the user with the username
"john_doe."
12. Deleting Data with the DELETE Statement:
The DELETE statement is used to remove rows from a table based on
specified conditions. The basic syntax is as follows:
DELETE FROM table_name
WHERE condition;
WHERE condition: This is the condition that specifies which rows to
delete. If omitted, all rows in the table will be deleted.
13. DELETE FROM users
WHERE username = 'john_doe’;
This query deletes the user with the username "john_doe" from the
table.