SQL常用查询:MySQL 8版本

SQL 语句

用户

创建用户

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'mysql';

CREATE USER 'user'@'%' IDENTIFIED BY 'mysql';

用户授权

GRANT ALL ON `test`.* TO 'admin'@'localhost' WITH GRANT OPTION;

GRANT SELECT,UPDATE,INSERT,DELETE ON `test`.* TO 'user'@'%' WITH GRANT OPTION;

取消用户权限

REVOKE DELETE ON `test`.* FROM 'user'@'%';

删除用户

DROP USER 'user'@'%';

数据库

登录数据库

mysql -u admin -p mysql

创建数据库

CREATE DATABASE `test` DEFAULT CHARACTER SET 'utf8' DEFAULT COLLATE 'utf8_bin';

查看该用户下所有的数据库

SHOW DATABASES;

切换数据库

USE test;

删库

DROP DATABASE `db`;

建表

CREATE TABLE `test`.`user`  (
  `id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(30) NOT NULL COMMENT '昵称',
  `pwd` varchar(32) NOT NULL COMMENT '加密后的密码',
  `gender` tinyint(1) NOT NULL COMMENT '性别',
  `profile` varchar(255) NOT NULL COMMENT '简介',
  PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = 'user';

删表

DROP TABLE `user`;

插入行

INSERT INTO `test`.`user`(`id`, `name`, `pwd`, `gender`, `profile`) VALUES (1, 'Linnzh', 'e10adc3949ba59abbe56e057f20f883e', 1, 'Linnzh是一个超级可爱超级聪明的绿孩子!!!');

删除行

DELETE FROM `test`.`user` WHERE `id` = 1;

更新行

UPDATE `test`.`user` SET `pwd` = 'c33367701511b4f6020ec61ded352059' WHERE `id` = 1;

创建索引

ALTER TABLE `test`.`user` 
ADD INDEX `idx_name`(`name`) COMMENT '昵称索引';
CREATE INDEX `idx_name` ON `test`.`user`(`name`);

删除索引

ALTER TABLE `test`.`user` 
DROP INDEX `idx_name`;
DROP INDEX `idx_name` ON `test`.`user`;

查询

单表查询

SELECT * FROM `test`.`user`;
SELECT `id`, `name` FROM `test`.`user` WHERE `gender` = 1;
SELECT count(*) AS `count` FROM `test`.`user`;

待续

posted @ 2019-09-17 17:26  Linnzh  阅读(416)  评论(0编辑  收藏  举报