基本使用
Windows下安装MySQL5.7
CentOS 7 通过 yum 安装 MariaDB
Centos 6.5下编译安装MySQL 5.6.14
连接服务器
mysql -h 127.0.0.1 -P 3306(默认链接3306端口) -u root -p
查看数据库连接情况
show processlist
数据库之间的切换
如果知道目标库名称可直接使用 : use XX;
以helloworld为例:
create database helloworld;
drop database helloworld;
use helloworld;
create table sudent(id int,name varchar(32), gender varchar(2));
insert into sudent (id,name,gender) values(1,'张三','男');
insert into sudent (id,name,gender) values(2,'李四','男');
insert into sudent (id,name,gender)values(3,'王五','女');
select * from sudent;
+------+--------+--------+
| id | name | gender |
+------+--------+--------+
| 1 | 张三 | 男 |
| 2 | 李四 | 男 |
| 3 | 王五 | 女 |
+------+--------+--------+
3 rows in set (0.00 sec)
创建数据库
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [, create_specification]
...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
create database db1;
//创建数据库没有指定字符集和检测规则时,默认使用默认字符集: utf8,校验规则为: utf8_general_ci
create database db2 charset=utf8;
//字符集主要控制数据库使用什么语言,utf8可以使用中文
create database db3 charset=utf8 collate utf8_general_ci;
字符集和校验规则
show variables like 'character_set_database';
show variables like 'collation_database';
show charset;
show collation;
create database test1 collate utf8_general_ci;
use test1;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
//查找结果:
MariaDB [test1]> select * from person where name='a';
+------+
| name |
+------+
| A |
| a |
+------+
2 rows in set (0.01 sec)
//排序结果
MariaDB [test1]> select * from person order by name;
+------+
| name |
+------+
| A |
| a |
| B |
| b |
+------+
4 rows in set (0.00 sec)
2.区分i大小写
create database test2 collate utf8_bin;
use test2
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
//查找结果:
MariaDB [test2]> select * from person where name='a';
+------+
| name |
+------+
| a |
+------+
1 row in set (0.00 sec)
//排序结果:
MariaDB [test2]> select * from person order by name;
+------+
| name |
+------+
| A |
| B |
| a |
| b |
+------+
4 rows in set (0.00 sec)
操作数据库
show databases;
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
//对数据库的修改主要指的是修改数据库的字符集,校验规则
表操作
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
1.field 表示列名
2.datatype :表数据
3.charactor set : 字符集,如果没有指定字符集,则以所在数据库的字符集为标准
4.collate : 如果没有指定校验规则,则以所在数据库的校验规则为准
create table users(
id int,
name varchar(20) comment '用户名',
password char(32) comment '密码是32位的md5值',
birthday data comment '生日'
)character set utf8;
查看表结构
desc users;
MariaDB [test1]> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.03 sec)
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename DROP (column);
插入assets字段,
alter table users add assets varchar(100) comment'图片路径' after birthday;
//对数据并没有影响
MariaDB [test1]> select * from users;
+------+------+----------+------------+--------+
| id | name | password | birthday | assets |
+------+------+----------+------------+--------+
| 1 | a | b | 2000-10-10 | NULL |
| 2 | c | d | 2010-10-10 | NULL |
+------+------+----------+------------+--------+
2 rows in set (0.00 sec)
MariaDB [test1]> desc users;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| assets | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
将name字段改为30
alter table users modify name varchar(30);
删除某一列
alter table users drop XX;
更改表名
alter table users rename to member;
将name列改为trait
alter table member change name trait varchar(40);//需要重新定义类型长度