[TOC]
摘要
-
安装完成后,配置文件:
-
使用locate my.cnf命令可以列出所有的my.cnf文件
-
如果没有配置文件,使用
mysql * --help|grep 'my.cnf'*
查看读取配置文件的默认目录 -
在配置文件中写入:
[client] default-character-set = utf8 [mysqld] default-storage-engine = INNODB character-set-server = utf8 collation-server = utf8_general_ci
utf-8支持中文编码
-
utf8可以换成utf8mb4,支持emoji字符
-
确定配置正确:登录MySQL之后,输入
show variables like '%char%';
-
基础语法
MySQL语句以分号(;)作为语句的结束。
启动
mysql -uroot -p
# 接着输入密码
创建数据库
create database 数据库名 [其他选项];
create database samp_db character set gbk; # 为了显示中文,指定字符编码为gbk
# 查看数据库
show databases;
# 选择操作数据库
# 1. 在启动时指定(主机名是远程的时候指定)
mysql -D 所选择的数据库名 -h 主机名 -u 用户名 -p
# 2. 在启动之后使用use指定
use 数据库名
# 删除数据库
drop database 数据库名
数据类型
数据库表
创建数据表、数据表的查看
# 就是创建表格最上面那一行的信息
create table 表名称(列声明);
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
# 注意:这个最后一行的末尾不能加逗号','
# 如果不在mysql中输入,将语句保存到createtable.sql文件中,然后在shell中输入:
mysql -D 数据库名 -u 用户名 -p < createtable.sql
# 查看已创建了表的名称
show tables;
# 查看已创建的表的详细信息
describe 表名;
MySQL关键字 | 含义 |
---|---|
NULL | 数据列可包含NULL值 |
NOT NULL | 数据列不允许包含NULL值 |
DEFAULT | 默认值 |
PRIMARY KEY | 主键 |
AUTO_INCREMENT | 自动递增,适用于整数类型 |
UNSIGNED | 无符号 |
CHARACTER SET name | 指定一个字符集 |
- blob、text、geometry、json不支持默认值设定
表的修改
对创建后表头的修改方式
添加列
alter table 表名 add 列名 列数据类型 [after 插入位置];
# 不加插入位置的话,就是在表的最后追加列:
alter table students add address char(60);
# 在age列之后插入:
alter table students add birthday date after age;
修改列
alter table 表名 change 列名 新列名 新数据类型;
# 将tel列改名为telephone:
alter table students change tel telphone char(13) default "-";
# 将name列的数据类型改为char(16):
alter table students change name name char(16) not null;
删除列
alter table 表名 drop 列名;
alter table students drop birthday;
重命名表
alter table 表名 rename 新表名;
删除表
drop table 表名;
数据处理
往表中插入数据
insert [into] 表名 [(列名1, 列名2, ...)] values (值1, 值2, ...);
# []内的内容是可选的
insert into students values (NULL, "王刚", "男", 20, "13811371377");
insert into students (name, sex, age) values ("xnchen", "女", 21);
查询表中数据
select 列名 from 表名 [可选查询条件];
# 例如,查询students表中所有的学生名字和年龄:
select name, age from students;
# 也可以用通配符*查询表中所有的内容:
select * from students;
# 查询特定条件
select 列名 from 表明 where 条件;
select * from students where age > 21;
select * from students where name like "%王%"; # 查询表中所有名字带有“王”的人的信息
select * from students where id<5 and age>20;
# 查看表长度
select count(*);
修改表中信息
update 表名 set 列名=新值 [where 更新条件];
update students set tel=default where id=5; # 将id为5的手机号改为默认的'-'
update students set age=age+1; # 将所有人的年龄增加1
update students set name="张" where tel="13288097888"; # 将手机号为13288097888的人的姓名改为张
删除表中信息
delete from 表名 where 删除条件;
delete from students where id=2;
delete from students wherea ge<20;
delete from students; # 删除所有信息
创建视图(view)
实际上就是创建联表
一个例子:
create view tagPost
AS
SELECT
postTag.tid as tid,
tagInfo.name as tagName,
post.pid as pid,
post.title as title
FROM ((tagInfo JOIN post) JOIN postTag)
WHERE (
(post.pid = postTag.pid)
AND
(postTag.tid = tagInfo.tid)
);