1.mysql 客户端连接认证: mysql -hPup h(指定主机)P(指定软件) u p认证
mysql -hlocalhost -P3306 -uroot -p
2.断开mysql连接 : \q
3.库操作
新增数据库 create database 数据表名 [库选项]:
库选项: 字符集设定: charset/character set GBK(or UTF8)
校对集设定:collate
create database test charset UTF8;
查询数据库
-- 查询全部数据库
show databases;
-- 模糊查询 ‘_’ 匹配一个zifu,“%”匹配多个
show databases like 'te%';
-- 查看数据库的创建语句
show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.04 sec)
修改数据库(仅限修改库选项)
alter database test charset GBK;
删除数据库
drop database test;
4.表操作
创建数据表
--create table 表名(
-- 字段名 数据类型,
-- 字段名 数据类型
-- )[表选项];
-- 表选项 : 字符集 校对集 存储引擎
-- 创建数据表时必须指定数据库,有两种方式:
-- 1. 在创建时指定数据表:create table test.tmp();
-- 2. 在创建前用use test 隐式的指定数据表;
use test;
create table t_tmp(
name varchar(10),
age int
)charset utf8;
查看数据表
-- 查看所有数据表
show tables;
-- 模糊查询
show tables like 't%';
--查看表的创建语句
show create table tmp\G;
-- 查看表结构
-- desc/describe/show columns
desc tmp;
describe tmp;
show columns from tmp;
更新数据表
-- 修改数据表本身
-- 修改数据表名 rename 表名 to 表名;
rename table tmp to t_tmp1;
-- 修改表选项
alter table t_tmp1 chatset utf8;
-- 修改数据表字段 新增 修改 重名 删除
-- 新增字段 alter table 表名 add [column] 字段名 数据类型 [列属性] [位置]
alter table t_tmp add class varchar(10);
-- 修改字段 (通常 属性 和 位置) alter table 表名 modify 字段名 数据类型 [属性] [位置]
alter table t_tmp modify class char(10) after school;
-- 重命名 alter table 表名 change 字段名 新字段名 数据类型 [位置][属性]
alter table t_tmp change school sch char(10) after class;
-- 删除字段 alter table 表名 drop 字段
alter table t_tmp drop sch;
删除数据表
drop table 表名1,表名2;
5.数据操作
插入数据
-- 插入数据方式1: insert into 表名 values 值列表;
insert into t_tmp1 values
('lili',12),
("wanwan",13),
("doghothero",12);
-- 插入数据方式2: insert into 表名 (字段名,字段名) values (值列表);
insert into t_tmp1 (age,name) values (14,"tianfei");
查看数据
-- select */字段列表 from 表名 [where 条件]
select * from t_tmp1;
select name,age from t_tmp1;
select name,age from t_tmp1 where age > 10;
更新数据
-- update 表名 set 字段=值 where 条件
update t_tmp1 set age=10 where age>12;
删除数据
-- delete from 表名 [where 条件];
delete from t_tmp1 where age<12;
6.中文数据问题
-- 查看所有字符集
show character set;
-- 查看服务器默认的对外处理字符集
show variables like 'character set %'
-- 修改客户端默认字符集
set charecter_set_client = GBK;
set names gbk;
7.校对集:数据比较方式
校对集的三种方式:_bin_binary 二进制比较 区分大小写
_cs: 大小写敏感
_ci: 大小写不区分
查看所有校对集:
show collation;
-- 排序
select * from t_tmp1 order by name asc/desc;