Database mysql add, delete, modify and check basic statement operations (basic statement miscellaneous)

First of all, we open mysql in the cmd of windows (or open mysql directly)
net start mysql5.7
. The statements of different versions are slightly different.
Start MySQL as the root user:

mysql -u root -p

View database

show databases;

The format of the database creation statement is create db_name;
here we create a database named caozuo, which is used for our operation

create database caozuo;

Then we select the database to be operated, so that our follow-up operation is for changing the database, and the
sentence format is use db_name.

use caozuo;

You can see the prompt message:
Insert picture description here
Then we first create a data table, here we create several tables, namely the user table, the vip user table, etc. (the code of the table is written in the terminal, and this code part retains redundant symbols):
Create a data table The format is CREATE TABLE table_name (column_name column_type);

create table user(
    -> name varchar(30),
    -> id char(20),
    -> sex char(2),
    -> primary key(id)
    -> )charset=utf8;  #用户表

> create table vipuser(
    -> name varchar(30),
    -> id char(20),
    -> vip_grade int,
    -> 胜加成 int,
    -> primary key(id,vip_grade)  #这里设两个主键略不合适,一个id可以标识
    -> )charset=utf8;
create table number(
    -> 数字 int,  primary key(数字)
    -> );

The format of the deleted data table is drop table table_name;
here we delete the number table

 drop table number;

! View the screenshot of the deleted table . Delete the table
. Insert data in the user table:
Before inserting, we first check the attributes of the table to ensure that our type is correct,
show columns from table;

show columns from user;

The
Insert picture description here
added data is displayed as follows : The
insert statement format is insert into table
(field1, field2,...fieldN)
VALUES
(value1, value2,...valueN );
here we first insert three pieces of data into the user table


mysql> insert into user
    -> (name,id,sex)
    -> values
    -> ("王五轩",0010,"男");
Query OK, 1 row affected (0.00 sec)

mysql> insert into user
    -> (name,id,sex)
    -> values
    -> ("李六六",0011,"男");
Query OK, 1 row affected (0.00 sec)

mysql> insert into user
    -> (name,id,sex)
    -> values
    -> ("张七七",0012,"女");
Query OK, 1 row affected (0.01 sec)

You can use the query statement select *from user; to view the data we inserted

select *from user;

The basic syntax description of inserting data in the vipuser table refers to the above,
first check the vipuser attributes

 show columns from vipuser;

Then add data based on attributes

 insert into vipuser
    -> (name,id,vip_grade,胜加成)
    -> values
    -> ("燕双鹰",9999,10,99);
Query OK, 1 row affected (0.05 sec)

mysql>                          )
mysql> insert into vipuser
    -> values
    -> ("李六六",0010,2,10);

As shown in the figure:
Insert picture description here

Update data: The
basic syntax format is UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
Here we update a name in vipuser.

 update vipuser set name="燕鹰鹰" where id=9999;

We can see the revised data
Insert picture description here
query data :
The general format of the statement is:
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
First query all the data in the table, the
statement format is select *from table

select *from user;

Query an item select columns from table;
for example, query the name in the user table:

select name from user;

The specific conditional statement in the query table The
syntax format is SELECT field1, field2,…fieldN FROM table_name1, table_name2…
[WHERE condition1 [AND [OR]] condition2…
Here we first query the VIP level 10 in the VIPuser table, the statement is as follows:

select name,id,vip_grade from vipuser where vip_grade=10;

From the user table, find out the same information in the two tables (user table and vipuser table). The statement is as follows:

 select user.id,user.name,sex from user,vipuser where user.name=vipuser.name;

The display is as follows:
Insert picture description here
delete data to
delete the entire table with drop, as an example was given when creating a table, the statement format is:
DROP TABLE table_name;

Delete data in the table, the statement format is:
DELETE FROM table_name [WHERE Clause]
Here we delete an item in the table

 delete from user where name="王五轩";

The result is shown in the figure: Insert picture description here
we write here first about basic sentences about MySQL additions, deletions, and changes.

Guess you like

Origin blog.csdn.net/qq_45701131/article/details/106106984