[2023] mysql basic usage and summary of all statements for table creation

1. Introduction to Mysql

  1. What is a database?

Mysql is a server used to manage many databases.

Its name is Mysql. Mainly used to manage, allocate, and release software for databases

 2. What is a database?

It is a folder for managing files ending in .frm.

This .frm file can only be parsed by the Mysql engine.

The data is stored in the .frm file

3. Table file

        The file ending with .frm, the data is stored in this file,

4. Keyword directory

Database base directory

show databases; 

Database base directory

show databases; 

  View current database

create database package name;

 Create database

use database name; 

Choose to use a specific database

show tables;

Check which tables the data has

DDL

create database name;

Create SKY database

show databases;

Query database

drop database database name;

Delete the database if it exists

Exit mysql error:

Ctrl--exit!

Import Data

source

2. Basic database execution commands

Command mind map

Any command needs to end with a semicolon

show databases; 

  View current database

create database package name;

 Create database

use database name; 

Choose to use a specific database

show tables;

Check which tables the data has

1. DDL: Create, modify, and delete databases and tables

Database operations

  1. grammar

create database name;

 create database sky;

Create SKY database

#If it does not exist, create a database and set the character encoding set; //if not exists: means if it does not exist; charset=utf8: set character encoding

mysql> create database if not exists zheng charset=utf8;

show databases;

Query database

drop database database name;

drop database if exists zheng;

Delete the database if it exists

Operations on tables

  1. grammar:

create table if not exits 表名(

Column name 1 data structure constraints,

Column name 2 data structure constraints,

)engine=InnoDB charset=utf8;

Example:

create table  if not exists  email(    //创建一个表 如果不存在的话 叫email
      codeKey varchar(50) primary key,           #该字段为主键(不允许重复且不为空)。
      username varchar(50) not null,             #所属用户,不允许为空
      content text,                              #文本类型
      sendTime datetime                          #发送时间
)engine=InnoDB charset=utf8;                 #设置当前表使用的是什么引擎及编码。

codeKey: column name; varchar(50): data type primary key: whether it can be null

Established successfully

Operations on data

For example: operations on adding, deleting, and changing data

  1. Query table structure desc table name;
    1. grammar:

desc table name;

desc email

desc(query)

  1. add a column
    1. grammar

alter table table name add column name data type;

alter table email add fu char(1);

add(increase)

  1. Modify column data type

 alter table table name modify column name new data type.

alter table email modify fu varchar(1);

modify (change data type)

  1. Modify column name

 alter table table name change original column name new column name data type

alter table dys1 change fu ping varchar(1);

change(column name)

  1. Delete a column

 alter table table name drop column name

 alter table dys1 drop ping;

drop(delete)

2. DML [for data] 

insert

 delete

update

increase

delete

change

  1. insert: Insert a piece of data.

grammar:

select * from table name; //query table information

insert into table name value | values ​​(value 1, value 2...); //full column insert

Example:

 insert into email values(uuid(),'[email protected]','《关于...》',now());

selective insertion

  1. update: change

grammar

update table name set column name 1=value 1, column name 2=value 2..where filter condition;

Example:

#Through update, change the codeKey (column) of dys1 (table) = '17119b72-c39a-11ec-a9e1-04d9f503c7e7', and change the username (account) to '[email protected]'

update dys1 set username='[email protected]' where codeKey =  31007d24-c3cc-11ec-b54b-b025aa25374b;

4. delete: delete

grammar:

 Table name where filter condition;

Example:

#Delete the data that changed the account to [email protected].

delete from email where username = "[email protected]";

3. Data type:

1. Numeric type

 tinyint

mini

one byte

127

smallint

small type

two bytes

326767

mediumint

medium type

three bytes

int

standard type

four types

bigint

large type

eight bytes

Example of creating a table

create table my_number(

    n1 tinyint,

    n1 smallint,

    n3 mediumint,

    n4 int,

    bigint,

);

Insert example

2, decimal type:

Classification into 3 categories:

 float

four bytes

single precision, imprecise precision

double

eight bytes

Double precision, precision around 15 lengths

decimal

Fixed point type

The precision is relatively high. Integers will not be rounded.

grammar

create table mu_decimail(

    f1 float(10,2),  //10:表示总长度最大10,小数点后最多2位,小数位长度不能超过前面的总长度

    d1 double(10,2),

    d2 becimal(10,2)

);

 3. String type:

        In mysql ' ' and " " both represent strings.

        Strings are divided into 6 categories:

 cher(20)

Fixed length type.

When creating a table, the disk will allocate 20 lengths of space, regardless of the length of the data

Always use 20 characters in length. (Example: ID number, mobile phone number...)

varcher(20)

Variable length type

When creating a table, the disk will allocate 20 lengths of space, and the final occupied space is determined according to the length of the data.

(Such as email password order number…)

text

text type

Generally, text is used for lengths greater than 255.

blob

binary text type

将媒介以二进制形式存放。文本类型显示的是文本本身的数据

enum

枚举类型

事先将数据提前定义,在插入的数据中,不能超出枚举定义的数据。

set

集合

与enum

举例:创建字符类型表;//s1:表示每一列的类型

举例:输入表里每个变量(1列)的数据

insert into my_string valies('aba','adadada',advd','adadadw','这个','aaf',);

4、日期类型

分为5大类:日期以字符串类型输入

 datetime

公元日期

date

日期

time

时间

year

年份

timestamp

格林威治:1970-01-01 08:00:00(之后才可输入)

now();时间函数:表示当前时间

举例

详细的针对数据的增删改查语句可以看我下一篇

Guess you like

Origin blog.csdn.net/weixin_52315708/article/details/131499943