MySQL学习小记(一)
大致练习一些常用的命令了,日后直接查,实在不想记这玩意hhh
dos窗口下mysql清屏
system cls;
添加新用户及密码
CREATE USER 'knight'@'localhost' IDENTIFIED BY '123456';
查看所有用户
SELECT user, host FROM mysql.user;
给本地的用户赋予特权
GRANT ALL PRIVILEGES ON * . * TO 'knight'@'localhost';
查看用户的特权
SHOW GRANTS FOR 'knight'@'localhost';
刷新MySQL的系统权限相关表,否则会出现拒绝访问
FLUSH PRIVILEGES;
新增数据库并新增员工表
CREATE DATABASE acme;
SHOW DATABASES;
USE acme;
CREATE TABLE users(
id INT AUTO_INCREMENT, #自动增加
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(75),
password VARCHAR(255), #加密后会很长
location VARCHAR(100),
dept VARCHAR(75), #部门
is_admin TINYINT(1), #布尔值
register_date DATETIME,
PRIMARY KEY(id) #主键值
);
工作台
MySQL :: Download MySQL Workbenchhttps://dev.mysql.com/downloads/workbench/
在刚才建的表中新增记录
SELECT * FROM users;
INSERT INTO users(first_name, last_name, email, password, location, dept, is_admin, register_date)
values ('Jack', 'Tong', 'jack@email.com', '666666', 'Beijing', 'development', 1, now());
# 新增多个记录
INSERT INTO users(first_name, last_name, email, password, location, dept, is_admin, register_date)
values ('Marry', 'Yu', 'mary@email.com', 'ghshtrh', 'Beijing', 'manager', 1, now()),
('Lora', 'Aba', 'lora@email.com', '547337', 'Tianjin', 'lawyer', 0, now());
查看字段
SELECT first_name, last_name, register_date FROM users;
查看指定的字段有哪些记录
SELECT * FROM users WHERE location='Beijing' AND dept='development';
SELECT * FROM users WHERE is_admin = 0;
删除记录
DROP FROM users WHERE id = 2;
(新增了几个记录,以免不够用来测试)
修改信息
UPDATE users SET email='lisi@gmail.com' WHERE id=5;
新增字段
ALTER TABLE users ADD age VARCHAR(3);
修改字段的数据类型
ALTER TABLE users MODIFY COLUMN age INT(3)
按照字段进行升序/降序
SELECT * FROM users ORDER BY last_name ASC;
SELECT * FROM users ORDER BY age DESC;
联系不同的字段
SELECT CONCAT(first_name, '-', last_name) AS 'Name', dept FROM users;
查看字段有哪些种类
SELECT location FROM users;
SELECT DISTINCT location FROM users;
查看指定范围有哪些人
SELECT first_name,age FROM users WHERE age BETWEEN 26 AND 30;
SELECT * FROM users WHERE dept IN('designer', 'staff');
匹配所有职位以***开头或者结尾的员工(NOT LIKE 是求不含***的)
SELECT * FROM users WHERE dept LIKE 'd%';
SELECT * FROM users WHERE dept LIKE '%op%';
SELECT * FROM users WHERE dept LIKE '%ff';
常用命令
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799029.html