语法:
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,create_specification] ...]create_specification:[DEFAULT] CHARACTER SET charset_name[DEFAULT] COLLATE collation_name
说明:
#? 大写的表示关键字#? [] 是可选项#? CHARACTER SET: 指定数据库采用的字符集#? COLLATE: 指定数据库字符集的校验规则?
create database db1;
说明:
说明:当我们创建数据库没有指定字符集和校验规则时,系统使用默认字符集: utf8 ,校验规则是:utf8_ general_ ci ?
创建一个使用utf8字符集的 db2 数据库
create database db2 charset=utf8; ?
创建一个使用utf字符集,并带校对规则的 db3 数据库
create database db3 charset=utf8 collate utf8_general_ci; ?
show variables like 'character_set_database' ;show variables like 'collation_database' ;
show charset;
字符集主要是控制用什么语言。比如utf8就可以使用中文。
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' );
区分大小写
创建一个数据库,校验规则使用utf8_ bin[区分大小写]
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' );
进行查询
不区分大小写的查询以及结果
mysql> use test1;mysql> select * from person where name= 'a' ;+------+| name |+------+| a ????????|| A???????? |+------+2 rows in set ( 0.01 sec)
区分大小写的查询以及结果
mysql> use test2;mysql> select * from person where name= 'a' ;+------+| name |+------+| a???????? |+------+2 rows in set ( 0.01 sec)
mysql> use test1;mysql> select * from person order by name;+------+| name |+------+| a???????? || A???????? || b???????? || B???????? |+------+
区分大小写排序以及结果:
mysql> use test2;mysql> select * from person order by name;+------+| name |+------+| A???????? || B???????? || a???????? || b???????? |+------+
show databases; ?
show create database 数据库名; ?
示例:
mysql> show create database mytest;+----------+----------------------------------------------------------------+| Database | Create Database ????????????????????????????????????????????????|+----------+----------------------------------------------------------------+| mysql | CREATE DATABASE `mytest` /*!40100 DEFAULT CHARACTER SET utf8 */ |+----------+----------------------------------------------------------------+
说明:
语法:
ALTER DATABASE db_name[alter_spacification [,alter_spacification]...]alter_spacification:[DEFAULT] CHARACTER SET charset_name[DEFAULT] COLLATE collation_name
说明:对数据库的修改主要指的是修改数据库的字符集,校验规则
mysql> alter database mytest charset=gbk;Query OK, 1 row affected ( 0.00 sec)mysql> show create database mytest;+----------+----------------------------------------------------------------+| Database | Create Database ????????????????????????????????????????????????|+----------+----------------------------------------------------------------+| mytest | CREATE DATABASE `mytest` /*!40100 DEFAULT CHARACTER SET gbk */ |+----------+----------------------------------------------------------------+
DROP DATABASE [IF EXISTS] db_ name; ?
执行删除之后的结果 :????????数据库内部看不到对应的数据库????????对应的数据库文件夹被删除,级联删除,里面的数据表全部被删注意:不要随意删除数据库 ?
语法:
# mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径
示例:将mytest库备份到文件(退出连接)
# mysqldump -P3306 -u root -p123456 -B mytest > D:/mytest.sql ?
mysql> source D:/mysql- 5.7 . 22 /mytest .sql ;
如果备份的不是整个数据库,而是其中的一张表,怎么做?
# mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql ?
同时备份多个数据库
# mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径 ?
语法:
show processlist;
示例:
mysql> show processlist;+----+------+-----------+------+---------+------+-------+------------------+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------+------+---------+------+-------+------------------+| 2 | root | localhost | test | Sleep | 1386 |???????? | NULL || 3 | root | localhost | NULL | Query | 0 | NULL | show processlist |+----+------+-----------+------+---------+------+-------+------------------+
语法:
CREATE TABLE table_name (field1 datatype,field2 datatype,field3 datatype) character set 字符集 collate 校验规则 engine 存储引擎 ;
说明:
field 表示列名datatype 表示列的类型character set 字符集,如果没有指定字符集,则以所在数据库的字符集为准collate 校验规则,如果没有指定校验规则,则以所在数据库的校验规则为准
create table users (id int ,name varchar ( 20 ) comment ' 用户名 ' ,password char ( 32 ) comment ' 密码是 32 位的 md5 值 ' ,birthday date comment ' 生日 ') character set utf8 engine MyISAM;
说明:
不同的存储引擎,创建表的文件不一样。users 表存储引擎是 MyISAM ,在数据目中有三个不同的文件,分别是:users.frm :表结构users.MYD :表数据users.MYI :表索引
?
备注:创建一个engine是innodb的数据库,观察存储目录
desc 表名; ?
?
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,columndatatype]...);ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,columndatatype]...);ALTER TABLE tablename DROP (column);
案例:
在users表添加二条记录
mysql> insert into users values ( 1 , 'a' , 'b' , '1982-01-04' ),( 2 , 'b' , 'c' , '1984-01-04' );
在users表添加一个字段,用于保存图片路径
l> alter table users add assets varchar ( 100 ) comment ' 图片路径 ' afterbirthday;
?
?插入新字段后,对原来表中的数据没有影响:
mysql> select * from users;+------+------+----------+------------+-------+| id | name | password | birthday | assets |+------+------+----------+------------+-------+| 1? ? | a |? ?b? ? ?|?1982-01-04?? ?| NULL?|<= 原来的数据仍然存在| 2? ? | b |? ?c?? ? | 1984-01-04? ? | NULL |+------+------+----------+------------+-------+
?修改name,将其长度改成60
mysql> alter table users modify name varchar ( 60 );
mysql> desc users;+----------+--------------+------+-----+---------+-------+| Field ????????| Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+-------+| id???????? | int ( 11 )???????? | YES |???????? | NULL | || name | varchar ( 60 ) | YES |????????? | NULL | |<= 长度变成 60| password | char ( 32 ) | YES |???????? | NULL | || birthday | date???????? | YES | ????????| NULL |?|| assets | varchar ( 100 ) | YES |? ? ?? | NULL | |+----------+--------------+------+-----+---------+-------+?
?
mysql> alter table users drop password;mysql> desc users;+----------+--------------+------+-----+---------+-------+| Field????| Type ????????| Null | Key | Default | Extra |+----------+--------------+------+-----+---------+-------+| id ????????| int ( 11 ) ????????| YES | ????????| NULL |? ? ? || name? ?| varchar ( 60 ) | YES | ????????| NULL |? ? ?|| birthday | date ???????? | YES |???????? | NULL |? ? ? ?|| assets | varchar ( 100 ) | YES | ????????| NULL |? ? |+----------+--------------+------+-----+---------+-------+
修改表名为employee
mysql> alter table users rename to employee; ?
?
?
mysql> alter table employee change name xingming varchar ( 60 ); -- 新字段需要完整定义
mysql> desc employee;+----------+--------------+------+-----+---------+-------+| Field???????? | Type? ? | Null? ? | Key | Default | Extra |+----------+--------------+------+-----+---------+-------+| id ????????| int ( 11 )????????? ? | YES | ?????????| NULL | || xingming | varchar ( 60 ) | YES | ????????| NULL | || birthday | date? ? ? ? ? ? ?? | YES |???????? | NULL | || assets | varchar ( 100 )? ?| YES |???????? | NULL | |+----------+--------------+------+-----+---------+-------+
?语法格式
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
示例:
drop table t1;?