MySQL
本文最后更新于 417 天前,其中的信息可能已经有所发展或是发生改变。

环境 Ubuntu 20.04 vmware

常用

select schema_name from information_schema.schemata;
select table_name from inforamtion_schema.tables where table_schema=database();
select column_name from information_schema.columns where table_schema=database();

安装

安装 mysql server

sudo apt-get update
sudo apt-get install mysql-server
mysql
mysql

第一次登录是无需输入密码

mysql -u root -p
mysql

设置 root 密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_pwd';
mysql

退出

quit
mysql

语法

DDL

创建新用户

CREATE USER 'jbn'@'localhost' IDENTIFIED BY 'your_pwd';
mysql

赋予权限

GRANT ALL PRIVILEGES ON *.* TO 'jbn'@'localhost' WITH GRANT OPTION;

查看所有 database

show databases;
mysql

创建新的 database

crearte database test_db;
mysql

查询创建 database 的语句

show create database test_db;
mysql

删除 database

drop database test_db;
mysql

使用 database

use test_db;
mysql

显示当前 database 所有 table

show tables;
mysql

显示某 table 的表结构

desc table_name;
mysql

查询某表的创建语句

show create table table_name;
mysql

创建 table

create table test_table(
    id int comment "num",
    name varchar(50) comment "name",
    age int comment "age"
) comment "user_table";

// id, name, age 为字段值
mysql

添加字段

alter table test_table add nickname varchar(20);
mysql

修改字段

alter table test_table change nickname username varchar(30) comment "username";

// alter table <table name> change <old name> <new name> <type> comment "<new name>";
mysql

删除字段

alter table test_table drop username;

// alter table <table name> drop <name>;
mysql

修改 table 名称

alter table test_table rename to rename_test_table;

// alter table <old table name> rename to <new table name>;
mysql

删除 table

drop table test_table;

// drop table <table name>;
// drop table if exist <table name>
mysql

删除 table 并重新创建(删除所有数据,但是不改变表结构)

truncate table test_table;

// truncate table <table name>;
mysql

DML

插入值

insert into test_table(id, name, age) values(1, "test", 18);

// insert into <table name>(<column 1, column 2, ...>) values(<value1, value2, ...>);
mysql

批量插入值

insert into test_table(id, name, age) values(2, "test2", 19), (3, "test3", 19);

// insert into <table name>(<column 1, column 2, ...>) values(<value1, value2, ...>), (<value1, value2, ...>), ...;

给全部字段插入值

insert into test_table values(2, "test2", 19);

// insert into <table name> values(<value1, value2, ...>);

批量字段插值

insert into test_table values(2, "test2", 19), (3, "test3", 19);

// insert into <table name> values(<value1, value2, ...>), (<value1, value2, ...>), ...;
mysql

修改数据

update test_table set name="test";
update test_table set name="test4" where id=1;

// update <table name> set <column_name>=<value> [where <condition>]
mysql
mysql

删除数据

delete from test_table where name="test";

// delete from <table name> [where <condition>]
mysql
mysql

DQL

查询

select * from test_table;
mysql
select id from test_table;
mysql
select id, name from test_table;
mysql
select id name from where id=3;
mysql
select id as 'num' from test_table;
mysql
select distinct name from test_table;
mysql

条件查询

select * from test_table where id = 1;
select * from test_table where id <= 3;
select * from test_table where age is null;
mysql
mysql
select * from test_table where id != 3;
select * from test_table where id <> 3;
mysql
select * from test_table where id < 3 and id > 1;
select * from test_table where id < 3 && id > 1;
mysql
select * from test_table where id = 1 or id = 2;
select * from test_table where id in(1, 2);
mysql
select * from test_table where name like "test1";
select * from test_table where name like "test_";
select * from test_table where name like "____";
select * from test_table where name like "%3";
mysql

聚合查询

select count(*) from test_table;
select avg(age) from test_table;
select max(age) from test_table;
select min(age) from test_table;
select sum(age) from test_table;
mysql

分组查询

select count(name) from test_table group by name;
select name, count(name) from test_table group by name;
select name, count(name) from test_table group by name having count(name) >= 2;
select name, count(name) num from test_table group by name having count(name) >= 2;
select name, count(name) num from test_table where id > 1 group by name having count(name) >= 2;
mysql

排序查询

select name from test_table order by age;
select name from test_table order by age asc;
select name from test_table order by age desc;
mysql
select * from test_table order by name, age;
select * from test_table order by name, age desc;

// 当 name 相同时按照 age 排序
mysql

分页查询

select * from test_table limit 1, 3;
select * from test_table limit 2, 3;
select * from test_table limit 4, 3;
select * from test_table limit 1, 6;
mysql

DCL

查询用户

use mysql;
select * from user;
mysql

创建用户

create user "test"@"localhost" identified by "test";

// create user "<username>"@"<host>" identified by "<password>";

// create user "test"@"%" identified by "test" 
// % 为各个主机都能操作 
mysql

修改用户密码

alter user "test"@"localhost" identified with mysql_native_password by "pass";

// alter user "<username>"@"<host>" identified with mysql_native_password by "<new pwd>"
mysql

删除用户

drop user "test"@"localhost";

// drop user "<username>"@"<host>"
mysql

权限控制

以普通用户 test 登录,查数据库 VS root 用户查数据库

show grants for 'test'@'localhost';

// show grants for "<username>"@"<host>";
mysql
授权
grant all on test_db.* to 'test'@'localhost';

// grant <privilage> on <database>.<table> to "<username>"@"<localhost>";
mysql
mysql
取消授权
revoke all on test_db.* from 'test'@'localhost';

// revoke <privilage> on <database>.<table> from '<username>'@'<host>';
mysql

Function

字符串函数

select concat("hello", "world");
mysql
select lower('TEST');
select upper('test');
mysql
select lpad('test', 10, '*');
select rpad('test', 10, '*');

// select [rpad | lpad]('<string>', <length>, 'string');
mysql
mysql
select trim('  hello world  ');

// 去除两头的空格
mysql
select substring('hello world', 3, 5);

// select substring('<string>', start, length);
mysql

数值函数

select ceil(4.5);

// 向上取整
mysql
select floor(4.5);

// 向下取整
mysql
select mod(5, 4);

// 取模
mysql
select rand();

// random(0, 1)
mysql
select round(4.98767890987, 5);

// 保留小数位
mysql

时间函数

select curdate();
select curtime();
select now();
mysql
select year("2022-11-13");
select month("2022-11-13");
select day("2022-11-13");
mysql
select date_add(now(), interval 70 day);
select date_add(now(), interval 70 year);
select date_add(now(), interval 70 month);
mysql
select datediff('2020-11-13', now());
mysql

流程控制

select if(1=1, "value1", "value2");
select if(1=2, "value1", "value2");
select ifnull("test", "default");
select ifnull("", "default");
select ifnull(null, "default");

// IF(value, true, false)
// IFNULL(value1, value2)
mysql
mysql
select id, (
    case name 
    when "test3" then "true" 
    when "test2" then "false" 
    else "null" end) 
 from test_table;

// select <column>, (case <column> when <"value"> then <"value"> else <"value"> end) from <table name>
mysql

Constraint

用于插入和修改字段时使用

创建表

create table user(
    id int primary key auto_increment comment "main key",       -- 主键,自增值
    name varchar(10) not null unique comment "username",        -- 不能为空,不重复
    age int check (age > 0 && age <=120) comment "user age",    -- 条件
    status char(1) default "1" comment "user status",           -- 默认值
    gender char(1) comment "user gender"                        --
) comment "user_table";
mysql

插入值

insert into user(name, age, status, gender) values('test1', 19, '1', 'm');
insert into users values("test2", 19, '1', 'w'),("test3", 21, '0', 'm');
mysql

进程已结束,退出代码0

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇