Getting Started with MySQL (a)
What is a database
As the name suggests, it is a database management data warehouse (system).
Database Category:
Relations with non-relationship:
Relations: a link between database tables and table - MySQL
Non-relational: no concept of the table - Redis, MongoDB
Memory and hard drive:
Hard Drive: permanent data storage - MySQL, MongoDB
Memory: High --Redis data access efficiency, memcache
sql and nosql:
sql: sql statement by the operation of the database
nosql: database operation is in the form of Key-Value (value is a record)
MySQL open tour
First, connect MySQL different world!
Open cmd:
# 游客登录(mysql 8 已取消游客登录)
mysql
# 账号密码登录 mysql -u用户名 -p密码
mysql -uroot -p
# 输入回车后再次输入密码,密码不会显示
# 连接指定服务器的mysql :mysql -h ip地址 -P 端口号 -u 账号 -p
mysql -hlocalhost -P3306 -uroot -p
# 登入后退出
quit
exit
MySQL View landscape
# 查看当前用户
select user();
# root下查看所有用户信息
select * from mysql.user \G # \G 可以竖行显示
# 修改密码
alter user user() identified by 'password' # 修改当前用户密码
mysqladmin -uroot -p123 -hlocalhost password 123456 # 未登录状态下修改
# 创建用户并赋予权限
# 8.0之前:
grant 权限们 on 数据库名.表名 to 用户名@主机名 identified by '密码';
# 8.0后 无法一步完成了
create user 用户名@主机名 identified by '密码';
grant 权限们 on 数据库名.表名 to 用户名@主机名 with grant option;
The basic operation of the database (obtaining initial skills)
# 查看已有数据库
show databases;
# 选择数据库
use 数据库名;
# 查看当前所在数据库
select database();
# 创建数据库
create database 数据库名 [charset=编码格式];
# 查看创建数据库的详细内容
show create database 数据库名;
# 删库跑路
drop database 数据库名;
The basic operation of the table (initial skills)
# 查看已有的表
show tables;
# 建表
create table 表名(字段名 数据类型(长度),...) [engine=引擎];
# 查看建表语句
show create table 表名;
# 查看表结构
desc 表名;
# 删表
drop table 表名;
Basic operation record (CRUD) (obtained basic skills)
# 查
select 字段们 from [数据库名.]表名 where 条件;
select * from mysql.user;
# 增
insert [into] [数据库名.]表名[(字段名...)] values (值...);
insert into stu values ('du',18);
insert into stu values ('lu',18),('jin',26); # 可同时赋值多条数据
# 改
update [数据库名.]表名 set 字段1=新值1, 字段n=新值n where 字段=旧值;
# 删
delete from [数据库名.]表名 where 条件;
# 清空表
truncate 表名;