#启动 mysql
systemctl start mysqld.service
#重启 mysql
systemctl restart mysqld.service
#停止 mysql
systemctl stop mysqld.service
#设置 mysql 开机启动
systemctl enable mysqld.service
#设置密码(mysql5.6 安装完成后,它的 root 用户的密码默认是空的)
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("这里输入root用户密码") where User='root';
mysql> flush privileges;
# 设置远程主机登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password';
# 执行以下命令,为root 用户添加远程登录的能力。
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "123456";
# 退出
exit/quit
常用命令集合
#查看当前所有库
show databases ;
# 创建数据库
create database innodb;
#删除库
drop database innodb;
# 选择库
use innodb
# 查看当前库中的表
show tables ;
# 创建学生表
create table Student (
s_id varchar(20) not null default '',
s_name varchar(20) not null default '',
s_birth varchar(20) not null default '',
s_sex varchar(10) not null default '',
primary key (s_id)
);
#删除表
drop table Student ;
# 查看表结构
desc Student;
# 插入数据
insert into Student (s_id, s_name, s_birth, s_sex) values ('02','钱电','1990-12-21','男');
#查询建表时的结构
show create table Student;
删除记录
delete from Student where id=1;
#更新
update Student set 字段=新值 where 条件;
删除表中的字段
delete from Student drop column 字段名;
alter table 表名 drop 字段名
主键约束:primary key
唯一约束:unique
非空约束:not null
默认约束:default
外键约束:foreign key(外键)references主表(主键)
查看别的数据库的表格:show tables from 表名;
排序:order by 字段名;(asc 升序、desc降序)
聚合函数:
sum() 函数:求累加和
count() 函数:同级行数数量
where条件
精确查询:=、!=、>、<、>=、<=
模糊查询:like(像)、not like(不像)
通配符:%:任意字符、-:单个字符
逻辑运算符:
and:同时满足(优先级大于or)
or:满足任意条件即可
区间运算:between a and b (闭区间)
集合运算:in 、not in
非空运算:is null 、is not null`