MySQL Getting Started Notes: How to download and install MySQL and how to use MySQL Workbench, what are the commonly used SQL statements

Introduction

MySQL is one of the most popular relational databases for Web Server.

download mysql

On the download page, click Archives(File) to select different versions of MySQL, and choose the version that suits your system. Tips: If your mac system has not been upgraded to 12, choose a version below 8.0.30.
insert image description here

After downloading, double-click to install, and follow the guide step by step.
But note: you must remember the password you set for the root user in this link, it will be used later! ! Be sure to remember the password! !
insert image description here

Download MySQL Workbench

MySQL Workbench is a visual database design software that provides visual design, model building, and database management functions for database administrators, program developers, and system planners. Through it, you can query, create and modify database tables in a visual way.

Using MySQL Workbench

After the download and installation is complete, open MySQL Workbench and click the plus sign to create a new connection.
insert image description here

Clicking the plus sign will pop up this interface:
Connection Name just pick one. Password is the password set when installing MySQL earlier. After entering the password, click ok.
insert image description here

Click the newly generated link to enter this interface:
click the symbol in the upper left corner, and a blank editing area will be created. You can enter the SQL statement in the editing area, and click the lightning symbol to execute the statement. You can also select a sentence and click the lightning symbol.
insert image description here

Click this symbol to create a new schema: enter the schema name and click apply.
insert image description here

It will automatically generate the corresponding SQL statement for you, just continue to click apply.
insert image description here

Right-click on the database you just created, select Create Table, and create a new data table. The table name and each column of the table can be edited on the right.
insert image description here

Configuration for each column:

Commonly used: PK: primary key; NN: not Null; AI: auto increment.

Others: UQ: unique unique index; BIN: binary binary data (binary data larger than text); UN: unsigned unsigned integer (non-negative); ZF: zero fill fills with 0, for example, the field content is 1 int(4) , the content is displayed as 0001; G: generated column generates a column.

varchar(45): The length of the string is set to 45; longtext: the maximum storage capacity is 4GB, which is suitable for super long text, such as articles.
Note: MySQL version 5 and above: varchar(45) means 45 characters, whether it is English Chinese characters or numbers.
insert image description here

Right-click on the newly generated table, the following menu appears: alter table: 修改表;drop table:删除表
insert image description here

Commonly used SQL statements

Example: (Note: -- Indicates comments)

// 使用某个数据库
use 数据库名称;
// 显示该数据库下所有的表
show tables; 

// 查询 users 表的所有列,*表示所有;考虑到mysql性能,尽量不要使用星号。
select * from users;
// 从 users 表查询出 id,username 列
select id,username from users;
// 查询 users 表所有列,查询结果根据 id 排序。
select * from users order by id;
// like '%keyword%'表示模糊查询,order by id desc表示根据id逆序排列。
select * from users where username like '%o%' order by id desc;
// 查询多个条件,用 and 连接
select * from users where username='gougou' and `password`='123';
// 查并集,用 or 连接
select * from users where username='gougou' or `password`='123';
// 查询 MySQL 的版本
select version();

// 在users表里插入一条数据。注:password 是 MySQL 关键字,所以用``包裹
insert into users(username,`password`,realname) values ('xiannv','cc','小仙女');

// 修改数据:将 username=‘gougou’ 的那条数据的 realname 改为‘大狗狗’
update users set realname='大狗狗' where username='gougou';
// 直接修改数据可能会报错。解决方法:输入 SET SQL_SAFE_UPDATES = 0,并执行
SET SQL_SAFE_UPDATES = 0;

// 从 user 表删除 username='xiaoxiannv' 这条数据
//(注意一定要跟条件,不要误把整个表删除)
-- delete from users where username='xiaoxiannv';
// 实际项目中,我们一般会通过修改状态来做软删除,用1表示有效,0表示被删。
update users set state='1' where username='xiannv';
// 展示的时候就只查询 state 不等于 0 的数据,就相当于做了删除:
select * from users where state<>'0';

You can do it to see:
insert image description here

Well, the introduction to MySQL is here first.

Guess you like

Origin blog.csdn.net/dongkeai/article/details/127469438