Question: How to create a MySQL database? Answer: Connect to MySQL server. Create database. Create table. Define the column types in the table. Specify column constraints. Insert data. Query data.
MySQL Database Creation Guide
Create Database
-
# #Connect to the MySQL server using the MySQL client.
Windows: Open a command prompt and type - mysql -u root -p
(where
root is the default user for MySQL,
-p option prompts you for a password).
Linux: Open a terminal and type - mysql -u root -p
.
-
Create a database.
Use the - CREATE DATABASE
statement, followed by the database name, for example:
CREATE DATABASE my_database;.
Create table
- ##Use the
CREATE TABLE statement to create a table .
For example: CREATE TABLE people (id INT AUTO_INCREMENT, name VARCHAR(255), age INT);- .
id- The column is a self-increasing integer that serves as the primary key of the table.
name- The column is a VARCHAR type that stores strings with a maximum length of 255 characters.
age- The column is of type INT that stores integers.
- #Define the column types in the table.
Common types include: -
INT: Integer
- VARCHAR(n): Variable length string (maximum Length is n)
- DATETIME: Date and time
- BOOL: Boolean value (true/false)
-
- Specify column constraints.
Constraints help ensure data integrity and accuracy.
- For example:
-
NOT NULL- : The column cannot contain null values.
UNIQUE- : The values in the column must be unique.
PRIMARY KEY- : The column is the primary key of the table.
Insert data
- Use
INSERT INTO statement inserts data into the table.
For example: INSERT INTO people (name, age) VALUES ('John', 30);- .
- #When inserting multiple rows of data, you can use the multi-value insert statement.
For example: INSERT INTO people (name, age) VALUES ('Mary', 25), ('Bob', 40);- .
Query data
##Use the - SELECT
statement to query the table The data.
For example:
SELECT * FROM people (select all columns in the - people
table).
You can use the
WHERE clause to filter the results. For example: - SELECT * FROM people WHERE age > 30
(selects all people older than 30 years old).
The above is the detailed content of How to create mysql database. For more information, please follow other related articles on the PHP Chinese website!