--中文是自己发挥的地方
 --只要涉及到表头的调整,都属于表的操作:表操作和数据库操作关键字一样,所以需要指定table 还是database
 --只有涉及到表内荣的操作,才是数据操作:就不需要 指定关键字table了,那几个关键字本来就是针对数据的
 =======================begin-整体操作-begin=================================
 --连接数据库:
     mysql -uroot -p123456
     mysql -uroot -p123456
 --查看MySQL版本号:
     sudo aptitude show mysql-server    -- 已安装
     mysql -V
 --MySQL启动:
     sudo /etc/init.d/mysql  start/stop/restart    -- 不同的Linux中,存储目录不同    
     sudo service mysql start/stop/restart        -- 不同的Linux中,启动方法相同
 --获取MySQL系统 字符集 :
     show variables like 'char%';
 --想永久设置需要修改 mysql 配置文件。character_set_client、character_set_connection、character_set_database(确切滴说:优先级高到底字段编码,表编码,库编码,最后才是系统编码character_set_server)、character_set_results  字符集统一。 
 --【me认为:client的数据,转三手才会真正存储:一手:character_set_client 二手: character_set_connection 三手:优先级高到底字段编码,优先级高的设置了编码,就不会使用低优先级的编码,表编码,库编码character_set_database,最后才是系统编码character_set_server
 --数据回到客户端:只需要一次转手:character_set_results】
     show variables like 'char%';
     /etc/mysql/mysql.conf.d 目录下的 mysqld.cnf。
     --【重点】结论:为保证不出现中文乱码,至少应该保证 客户端、连接、服务器、结果集 使用的 字符编码一致。 —— utf8;
 --使用新数据库:
     use db1
 ===============end-整体操作-end=====================================================================begin-数据库的CRUD create+drop+alter+show-begin=============================
 --创建:    语法:    
     create database [if not exists] `数据库名` charset=字符集;
     --eg:
     create database if not exists `mydb1` charset=utf8;
     create database mydb2;    -- 默认的字符集为 latin1
     --[if not exists] 不添加:如果数据库已经存在报错。
     --反引号:区分大小写。 数据库名中包含特殊字符、关键字时,必须要添加。
     --字符集:GBK、GB2312、utf8、 utf8mb4 —— emoji标签
 --删除:    语法:     
     drop database [if exists] `数据库名`;    
 --修改:    语法:     
     alter database `数据库名` charset=字符集
 --查询:     
     show databases;     --查询MySQL中的所有数据库
     show create database sys;    --查询 ‘sys’数据库的创建语句。
 ==============end-数据库的增删改查-end=============================================================begin-表的操作CRUD create+drop+alter+show+desc-begin=============================
 --创建表:
     create table [if not exists] `表名` (
             字段名 数据类型 [null|not null] [auto_increment] [primary key] [comment 注释] , 
             字段名 数据类型 [default 默认值]…
             ……
             primary key(字段名1,字段名2)--创建主键,只可以有一个主键  primary key(字段名1,字段名2)联合主键
             unique(字段名1,字段名2),--组合为一件
             unique(字段名1),
             unique(字段名2)
     ) engine=存储引擎;
     create table 数据库名.表名(……);
     --eg:
     create table if not exists `teacher`(
     id int(9) auto_increment primary key comment '主键',
     name varchar(20) not null comment '姓名',
     phone varchar(11) comment '电话',
     `addr` varchar(100) default '地址不详'
     )engine=Innodb charset=utf8;    
     --eg:
     create table t1(t1 int, name varchar(20));
 --复制表:
     --复制表数据,不复制表属性(自动增长,主键)
     create table 新表名 select * from 旧表名;
     --复制表属性,不复制表数据。
     create table 新表名 like 旧表名;
     --补充数据:
     insert into 新表名 select * from 旧表;
 --删除表
     drop table [if exists] 表1,表2,… ;
 --eg:
     drop table 表名1,表名2;
 --修改表:添加字段 修改字段名字/类型 修改表名 删除字段 调整字段顺序
     alter table 表名 关键字 字段名;
     --添加一个字段:
     alter table 表名 add 字段名 字段类型 first;    
     alter table 表名 add 字段名 字段类型 after 另外一个字段
     --修改字段名、类型:
     alter table 表名 change 字段原名字 字段新名字 新字段类型;
     --修改主键
     alter table 表名 add primary key(字段1,字段2),add unique(字段1,字段2);
     alter table t19 change id id int primary key;
     --修改类型:
     alter table 表名 modify 字段名 字段类型定义
         --调整表的顺序 放语句最后,添加字段 修改字段名类型 时候:
         after 字段名||first
     --删除表的字段
     alter table 表名 drop column 字段名    ;
     alter table 表名 drop primary key||drop index 字段;--删除主键属性
     --表的重命名   
     alter table 原表名 rename 现表名;||rename table t1 to t2;    ||      alter table t2 rename to t3;    
     --修改引擎:
     alter table 表名 engine=myisam;
 --查看表:    
     show tables;--显示数据库中包含哪些表。
     show create table 表名;--查看创建表语句
     show create table 表名\G --按 垂直形式,显示表数据。(查看字段数量较多的表时,使用)
     desc 表名;    --查看表结构。
 ==================end-表的操作-end================================================================begin-数据操作CRUD insert into表名+delete from表名+update表名set+select字段名from表名【不再需要table关键字】-begin=========
 --插入记录:
     --插入一条记录
     insert into 表名(字段名1,字段名2...) values(值1,值2...);  
     --插入多条记录 插入自增的值用null,插入默认值用default
     insert into 表名 values(值1,值2...)【,(值1,值2...)】...;        
     insert into 表名(字段1,字段2...) values (值1,值2...),(值1,值2...),(值1,值2...)...;
 --删除记录:--delete只是断开inode trunct把空间还给mysql????????????
     delete from 表名 [where 条件1[ or 条件2]...];    --不释放表空间,自增键继续
     truncate 表名;        --把一个表清空,与 delete from 表名;的区别是,truncate摧毁重建
 --更新记录:
     --其中条件:条件之间用 or 字段=值 or Xx--更新一个字段或者多个字段的值,字段值可以=default
     update 表名 set 字段名=字段值[,字段名=字段值]... [where    条件1[ or 条件2]...];
 --查询记录:
     select * from 表名;
     select 字段名1,字段名2 from 表名 where 条件;
 =======================end-数据操作-end=====================================
 =======================begin-字段设计=======================================
 =======================begin-字段类型-字段占据空间大小,大小限制:数值类型,字符类型,日期类型
 --值类型:整型
     tinyint    smallint    mediunint    int bigint
     tinyint 1字节 unsigned:0-2^32-1        -127-128
     int 4字节 unsigned 0-2^32-1
         --desc 表名:int(5) 这里是显示宽度,至少这些宽度,大于这个宽度按照实际显示
     --值类型:浮点型
     float(4字节) double(8字节) 
         --浮点数声明 float(总位数,小数位数) 小数位超出四舍五入,整数位超出报错
     --值类型decimal
     decimal定点数,无精度丢失,两部分存储,内存空间变大
 --文本类型:字符型
     char(固定长度,长度是字符个数)定长效率高上限255字节 varchar(不能超过的字符数,字符字节数上限65535) 至于每个字符占几个字节,看编码;
 --文本类型:地址类型:
     text类型,存的是文本的地址。大段文本
     变长 tinytext text mediumtext longtext:大段文本
 --文本类型:枚举类型:计数从1开始【注意】:枚举类型,要么插入每局的值,要么插入值的序号,存储的时候是按照序号存储的。
     enum(值1,值2.)
 --文本类型:集合类型:计数从1开始,第二个乘以2,依次类推
     set(v1,v2) 
 --日期类型:
     datetime:年月日时分秒 date:年月日 time:时分秒 timestap: 
 =======================end-数据类型
 =======================begin-列属性-对字段样式的限制,not null,default XX,auto_increment,unique,comments XX
 --null  not null
 --default 插入默认值值用default代替
 --auto_increment这个属性的字段必须指定为 primary key    插入这个值用null
 --primary key:一个标志可以有一个主键,可以是联合主键,作用:检索
         --自增一定是主键,必须是主键
         --自增的主键,drop不了主键属性
         alter  table 表名 drop primary key;
 --unique:因为可以有多个唯一键,所以最好重命名
     --创建方式1,
                 creat table-起名字,id int unique或者unique(字段名) || 不起名字:unique UQ_字段名(字段名) 
     --创建方式2:
                 alter table 表名 add unique UQ_字段名(字段名);--也可以不起名字
                 alter table t23 add unique(name, email);--组合唯一键
     --删除方式: 
                 alter  table 表名 drop index 唯一键的键名;
 --创建索引:
     alter table 表名  add index(字段名)
     create table 表名(
         字段名 类型 ,
         index 索引名(字段名)
     )
 --添加外键:
     create语句时候:【constraint 别名 】foreign key (字段名) references 主表名 (主表字段) on delete set null on update cascade????????????try 
     alter时候:alter table 从表 add foreign key (从表的公共字段) references 主表(公共字段)
     alter table 表名 drop foreign key 外键名
     --从表不要设置成主键,因为不方便 在主表删除时候置空。因为主键不能为空
 --实体之间的关系:
     一对一,主表1 与从表2 主键对主键;通过主表1的主键和从表2的主键关联(我认为从表唯一键对应主表主键就行,涉及可以把从表置空)
     一对多:主表的主键与从表的非主键建立关系
     多对多:抽取出来两个表的主键,用第三张表保存主键的关系
 --数据库设计的三范式:
     --第一范式:数据不可再分;第二范式:数据描述的是同一个事情,非主键都应与主键有关联;第三范式:非主键字段中不要有依赖传递。=======================end-列属性
 =======================end-字段设计=======================================