创建用户和授权
创建用户
- 创建用户
create user fuqiang@localhost;
-- 设置密码
use mysql;
update user set password=password('123456') where user='fuqiang';
-- 一条数据创建用户并设置密码
create user jack@localhost identified by '123456';
用户授权
-- 给某数据库中的所有表授权,增删改查
grant select,insert,update,delete on 数据库.* to fuqiang@localhost;
取消授权
-- 取消插入的权限
revoke insert on database.* from fuqiang@localhost;
flush privileges;
远程授权
grant all privileges on *.* to '用户名'@'%' identified by '密码' with grant option;
flush privileges;
数据库操作
创建数据库并设置编码
show databases;
-- 创建数据库
create database if not exists vod default character set 'utf8';
use vod;
show variables like 'character_set_database';
Variable_name | Value |
character_set_database |
utf8mb4 |
show create database vod;
Database | Create Database |
vod |
CREATE DATABASE vod /!40100 DEFAULT CHARACTER SET utf8mb4 / |
alter database vod character set 'gbk';
数据表操作
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`password` varchar(128) DEFAULT NULL,
`addtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
show create table taocan;
desc user;
alter table user add age INT(11);
alter table user MODIFY age TINYINT(4) default 10;
alter table user change age ages tinyint(4) default 20;
alter table user modify addtime datetime after age;
alter table user age tinyint(4) default 25;
alter table user alter age drop default;
alter table user add primary key(id);
-- 先删除自增
alter table user modify id int(11);
-- 删除主键
alter table user drop primary key;
alter table user add unique(name);
alter table user drop index name;
alter table user drop age;
-- innodb 引擎
alter table user engine=innodb
-- myisam 引擎
alter table user enginr=myisam
alter table user auto_increment = 100;