MySql*第一部分(数据库,用户管理,授权管理,数据库操作,table操作,数据类型,表约束及使用)

Mysql了解

DB(DataBase):数据库   
DBMS:数据库管理系统
数据库系统组成:数据库、数据库管理系统、数据库应用程序.
数据存储在表(table)中

数据定义语言DDL:数据库,表操作 -- drop create  alter
数据操作语言DML:表内容操作 -- insert update delete
数据查询语言DQL:条件查询 -- select 
数据控制语言DCL:事务,一致性操作 -- grant(授权) revoke(取消某些权限) commit rollback

; 结束. 
/*------*/:多行注释
#:注释,到行尾! mysql链接:mysql -u用户名 -h主机ip -P端口号 -p密码 P端口一般默认为3306

  

 


用户管理

1. select user.host from mysql.user; 查看用户和主机号
or : use mysql;select user.host from user;

2. 创建用户: create user 用户@ip identified by '密码';
  /*  用户@ip  用户只能在该ip下才能登陆
    用户@192.168.1.%  用户只能在该ip段才能登陆
    用户@%   用户可以在任意ip下都能登陆(默认ip地址为%)
    */
    
3. 用户登陆
mysql -u用户 -h ip地址 -P 端口 -p密码

4. 修改用户
rename user 用户@主机ip to 新用户名@ip;

5. 删除用户
drop user 用户@主机ip地址;

6. 修改密码
alter user 用户名@主机ip identified by '密码';
or : alter user 用户@主机ip identified with mysql_native_password by '密码';

  


授权管理

查授收权限
show grants for 用户@主机ip;  #查看权限
grant 权限 on 数据库名.表名 to 用户@主机ip;  @授权
grant 权限 on 数据库名.表名 to 用户@主机ip with grant option;   #允许该用户给其他用户授被赋予的权限
revoke 权限 on 数据库名.表名 from 用户@主机ip #收回权限.  
/*  DB.*    #选择数据库中的所有表
    DB.表名   #选择数据库的某张表
    *.*    #所有数据库的所有表
    */

 


数据库及表操作

数据库创建:create database [if not exists] 数据库名;  #中括号内使用时表示该创建数据库是否已存在,使用不报错
查看数据库创建信息:show create database 数据库名;
使用数据库(当前):use 数据库名;
删除数据库:drop database 数据库;

查看表创建: show create table 表;
修改表名称: alter table 旧表名 rename 新表名;
修改字段: alter table 表 change 旧字段 新字段 新数据类型;
修改字段数据类型: alter table 表 modify 字段名 新数据类型;
表-添加字段:alter tableadd 新字段 数据类型 [约束条件][first][after]  #frist,可选参数.字段排在表的第一个;after,可选参数。将新字段添加到指定字段后面-删除字段: alter tabledrop 字段;
字段位置移动方法: alter table 表 modify 字段 数据类型 [first][after 字段(移至该字段后)]  #同上👆
表的删除:drop table 表; #此为删除无外键链接的表。

 


常用数据类型

Char(长度)        定长字符串,存储空间大小固定,适合作为主键或外键
Varchar(长度)        变长字符串,存储空间等于实际数据空间
Double(有效数字位数,小数位)        数值型
Float(有效数字位数,小数位)            数值型
Int(长度)        整型
Bigint(长度)    长整型
Date        日期型
Blob        二进制大对象(Binary Large OBject)
Clob        字符大对象(Character Large OBject)

 


表的约束及用法

primary key            主键约束,唯一标识对应的记录
    /* 表字段自动增加: 字段 数据类型 auto_increment;
    添加主键自增:alter table 表 add 字段 数据类型 auto_increment primary key;
    修改非主键为主键自增: alter table 表 modify column 字段 数据类型 auto_increment primary key;
    删除主键自增:alter table 表 modify 字段(原为主键) 数据类型;
    */
foreign key  外键约束
not null     非空约束
unique       唯一约束
default      默认值约束,用于设置字段的默认值

单字段主键:字段 数据类型 primary key
多字段主键:primary key(字段1,字段2,...)
表添加主键: alter tableadd primary key(字段);
表删除主键:alter tabledrop primary key; #主键没有递增
or: alter table-modify 字段 数据类型,drop primary key; #主键有递增、没递增
其他约束使用modify来重新定义字段,进行约束清除!
唯一约束添加:alter tableadd unique(字段);
唯一约束删除:alter tabledrop index 索引名; #索引同字段名相同,drop索引,不是字段
or: 查找表索引:show index from 表; #如果不知道有哪些索引,show index可查
默认值设置:alter tablealter column 字段 set default 设置默认值;
默认值修改:alter tablealter 字段 set default 值;
默认值约束删除:alter tablealter column 字段 drop default;

 

posted @ 2020-03-20 22:46  逍遥大帝  阅读(175)  评论(0编辑  收藏  举报