MySQL 8.0常用SQL查询

2021/4/18 19:55:15

本文主要是介绍MySQL 8.0常用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`;

本文转载自:https://www.cnblogs.com/linnzh/p/11535591.html



这篇关于MySQL 8.0常用SQL查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程