Ten years of JAVA moving bricks - MYSQL foundation

Log in

mysql -h 主机名 -P端口 -u xxx -p xxx 

Sign out

exit

view version

mysql --version
mysql -V
select version();

view all databases

show databases;

use database

use xxx;

view all tables;

show tables;
查看数据库编码
show variables like '%char%';

1. One-line function

select length('我是');

select upper(substr('dsd愁是是打发点', 1, 3)) out_put;

select instr('杨不悔殷六侠', '六') out_put;

select rpad('yishushu', 10, '3');

select ceil('0.1456456');

select monthname(now());

select str_to_date('2020-3-2', '%Y-%c-%d') as daes;

select date_format(now(), '%Y/%m/%d');

select database();

select if(10 > 6, 'da', 'xiao');

select price, bid,
       case bid
           when 1 then price*1
           when 2 then price*2
           when 3 then price*3
           when 4 then price*4
           when 5 then price*4
           end as 'newPrice'
from book;

2. Grouping function;

select sum(price) from book;
select avg(price) from book;
select max(price) from book;
select min(price) from book;
select count(price) from book;

group by  having (分组后的条件)

SQL99 syntax

内连接 table1 inner Join  table2 on xxx where xxx ;

外连接select * from table1 left outer join table2 on xxx

全外连接 table1 full outer join table2 on xxxx(并集)

交叉连接是笛卡尔积 cross join

Table structure modification

修改列名name  -> bookName
alter table book change column name bookName varchar(150);

修改数据类型 将type 类型改为varchar(20)
alter table book modify type varchar(20);

删除列
alter table book drop column totalPage ;

修改表名
alter table books rename to book;

复制表结构没有数据
create table bookCopy like book;

复制表结构有数据
create table bookCopy1 select * from book;

设置默认值 给Note字段
alter table visitor_application modify column note varchar(10) default '-1'

type of data

整形
tinyint, smallint,mediumint, int/interger, bigint;
int 类型长度代表默认填充0的个数,不是数据长度。和zerofill 搭配使用,才显示效果。
create table dep(
    id int(7),# 有符号
    name varchar(25),
    salary int(10) unsigned #无符号
)
小数
1.浮点型
float(M,D),double(M,D)
2.定点行(精度较高,如货币)
decimal(M,D)
M总长度,D小数点长度。可以省略
枚举类型

字符
char(M)  M为0~255之间的整数,M为最多多少字符 (固定长度的字符)
varchar(M)  M为0~65535之间的整数,M为最多多少字符 (可变长度字符	)
枚举类型
enum('a','b','c')只能拆入a,b,c 其一
集合类型
set('a','b','c','d') 可以插入a,b,c,d 的组合

时间类型
date 只保留日期
time 只保留时间
year 只保留年

datetime保存日期+时间  8个字节 1000-9999年不受时区影响
timestamp 保存日期+时间 4个字节 1970-2037 受时区影响。

common constraints

NOT NULL 非空约束,保证字段不为空
default 默认值约束
primary key 
unique 唯一约束,保证值具有唯一性,可为空
check 检查约束,(MySQL不支持)

标识列。自增长列。

affairs

默认是自动提交。查看事务级别select @@transaction_isolation->MySQL 8

禁用事务当前会话
1.set autocommit = 0;
start transation;(可选);
2.事务的语句(select insert update delete)
3.结束事务 commit;

事务类型
read uncommitted 读未提交(脏读,不可重复读,幻读都有),
read committed  读已提交,(没有脏读,其他不能避免),
repeatable read 可重复读 (还有幻读),
serializable 序列化 (解决所有问题),

Guess you like

Origin blog.csdn.net/weixin_43485737/article/details/132277689