Home > Database > Mysql Tutorial > body text

How to create mysql database

下次还敢
Release: 2024-04-22 20:06:34
Original
398 people have browsed it

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.

How to create mysql database

MySQL Database Creation Guide

Create Database

  1. # #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.
  2. Create a database.

      Use the
    • CREATE DATABASE statement, followed by the database name, for example: CREATE DATABASE my_database;.

Create table

  1. ##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.
  2. #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)
  3. 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

  1. Use

    INSERT INTO statement inserts data into the table.

    For example:
      INSERT INTO people (name, age) VALUES ('John', 30);
    • .
  2. #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
  1. 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!