mysql

一、Mysql支持以下约束:

 

※  主键约束 :primary key 

                      auto_increment 递增

※  唯一性约束:unique 

※  外键约束:foreign key

                       refernces  表 字段

※  非空约束:not null

※  默认值约束:default

添加主键约束

alter table 表  add primary key;

 

修改主键约束

alter table 表 modify id int primary key;

删除主键约束:

alter table 表  drop primary key;

 

类型 大小 范围(有符号) 范围(无符号) 用途
TINYINT 1 字节 (-128,127) (0,255) 小整数值
INT或INTEGER 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
BIGINT 8 字节 (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值

日期和时间类型

DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD 日期值
TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS 时间值或持续时间
YEAR 1 1901/2155 YYYY 年份值

 

字符串类型

类型 大小 用途

CHAR 0-255字节 定长字符串

VARCHAR 0-65535 字节 变长字符串

TEXT 0-65 535字节 长文本数据

LONGTEXT 0-4 294 967 295字节 极大文本数据

 

 


函数
floor       取整数
ceiling      有数就加一
round          四舍五入
truncate      去小数位数
count        总数
avg        平均数
max       最大
min        最小
sum       总和
ltrim       去掉前面空格
trim       去掉空格
length      长度

delimiter               修改结束符号

 

触发器

create trigger 名字   after delete   on   table1 for each row  动作  创建触发器

show triggers \G      查看触发器

 

索引

alter table 表 add index (字段);             添加普通索引

alter table 表  drop index字段;            删除

create index 索引名称 on 表名(字段名称(长度))
例:
create index age_index on create_index(age);

 唯一索引

alter table 表 add unique stu(num);

 

修改权限密码

 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123';

 flush privileges;

 

我用docker启动了俩台数据库 主的端口是3306 从的是3307

[root@zxw17 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                      NAMES
fd28cd2d031b        mysql               "docker-entrypoint.s…"   12 minutes ago      Up 12 minutes       33060/tcp, 192.168.126.17:3307->3306/tcp   mysql1
052d9e9d1afd        mysql               "docker-entrypoint.s…"   2 days ago          Up 5 hours          192.168.126.17:3306->3306/tcp, 33060/tcp   mysql
这是启动命令

docker run --name mysql -p 192.168.126.17:3306:3306  -e MYSQL_ROOT_PASSWORD=123 -v /zxw:/var/lib/mysql -d mysql
第三方连接修改权限

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


 

 第一步登录到主的数控查看内容

复制代码
[root@zxw17 gq]# docker exec -it mysql1 bash
root@fd28cd2d031b:/# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.16 MySQL Community Server - GPL

mysql>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+

复制代码
3306数据库

复制代码
[root@zxw17 ~]# docker run -it mysql mysql -uroot -p123 -h192.168.126.17
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.16 MySQL Community Server - GPL

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zxw                |
+--------------------+
复制代码
第二步:修改配置文件

log-bin=mysql-bin-master  #开启二进制文件
server-id=1               #数据id
binlog-do-db=test         #可以同步的库
binlog-ignore-db=myslq    #不可以同步的库

 

posted on 2019-11-21 15:35  我就是我没毛病  阅读(126)  评论(0编辑  收藏  举报

导航