How To Create a MySQL Database, Tables and Insert Data

https:/‮l.www/‬autturi.com
How To Create a MySQL Database, Tables and Insert Data

To create a MySQL database and tables, and insert data, follow these steps:

  1. Connect to the MySQL server:
mysql -u username -p

Replace username with your MySQL username. You will be prompted to enter your password.

  1. Create a database:
CREATE DATABASE databasename;

Replace databasename with the name of the database you want to create.

  1. Select the database:
USE databasename;
  1. Create a table:
CREATE TABLE tablename (
  column1 datatype,
  column2 datatype,
  ...
);

Replace tablename with the name of the table you want to create, and column1, column2, etc. with the names of the columns in the table and their corresponding data types. For example:

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  salary DECIMAL(10,2)
);
  1. Insert data into the table:
INSERT INTO tablename (column1, column2, ...)
VALUES (value1, value2, ...);

Replace tablename with the name of the table, and column1, column2, etc. with the names of the columns in the table. Replace value1, value2, etc. with the values you want to insert. For example:

INSERT INTO employees (id, name, salary)
VALUES (1, 'John Smith', 50000);

You can insert multiple rows at once by using multiple VALUES clauses:

INSERT INTO employees (id, name, salary)
VALUES (1, 'John Smith', 50000),
       (2, 'Jane Doe', 60000),
       (3, 'Bob Johnson', 40000);
  1. Verify the data:

To check that the data was inserted correctly, you can use the SELECT statement to retrieve it from the table:

SELECT * FROM tablename;

Replace tablename with the name of the table. This will return all rows and columns from the table.

Note: These are just the basic steps for creating a database, tables, and inserting data in MySQL. There are many other features and options available, such as constraints, indexes, and more. Consult the MySQL documentation for more information.

Created Time:2017-10-28 21:38:50  Author:lautturi