MySQ基本操作

来源:互联网 发布:曹县大集淘宝村 编辑:程序博客网 时间:2024/06/05 15:15

MySQL数据库的基本操作

1.创建数据库

语法: create database 数据库名字 charset utf8;

数据库的名字最好别用关键字或者保留字(如database),如果非要使用,需加反引号“

如果需要创建中文名称的数据库,需要先将字符集设置为gbk后再创建,否者会出现乱码,这是因为客户端默认的字符集为GBK的,每次读取2个字节,而服务器默认的字符集是UTF8的,每次读取3个字节,设置gbk字符集语法:set names gbk;每一个数据库对应的目录下面都有一个opt文件,里面存放的是字符集和校对集,校对集依赖于字符集,字符集改变了,校对集一定会改变.

2.查看数据库

查看所有数据库show databases;查看指定部分的数据库:模糊查询show databases like 'pattern';      pattern---模型%: 表示匹配多个字符_: 表示匹配单个字符查看数据库创建语句show create database 数据库名字;

3.数据库的修改

数据库名字不可以修改修改数据库字符集alter database 要修改的数据库名字 charset 字符集类型(GBK/UFT8...);charset / character set [=] 字符集collate 校对集

4.删除数据库

drop database 数据库名字; 

5.新增数据表

1)显式的指定表所属的数据库

create table if not exists 数据库名.表名(字段名字    数据类型,字段名字    数据类型        ---最后一行不需要逗号)[表选项];

2)隐式的指定表所属的数据库,先进入某个数据库,再创建表,这个表就属于这个数据库

先进入要创建表的数据库     use 数据库名;然后再创建表:create table 表名(字段名字    数据类型,字段名字    数据类型        ---最后一行不需要逗号)[表选项];

6.查看数据表

1)查看所有数据表show tables;2)查看部分表:模糊匹配:show tables like 'pattern';3)查看表的创建语句show create table 表名;例如: show create table student\g     -- \g等价于;     show create table student\G      -- /G将查到的结构旋转90度变成纵向4)查看表结构(3种方法)desc 表名;    describe 表名;show columns from 表名;

7.更新数据表

表本身可以修改:表名和表选项  修改表名rename table 旧表名 to 新表名;修改表选项:字符集,校对集,和存储引擎alter table 表名 表选项 = 值;新增字段:alter table 表名 add column 字段名 数据类型 [列属性] 位置;   -- column可有可无修改字段:通常是修改属性或者数据类型alter table 表名 modefy 字段名 数据类型 [属性] [位置]; 重命名字段:alter table 表名 change 旧字段名 新字段名 [属性] [位置];  -- 如果不改变位置,可以不写位置删除字段(不可逆):alter table 表名 drop 字段名;删除数据表(不可逆):drop table 表名1,表名2...;   -- 可同时删除多个表

8.新增数据

方案1: 给全表字段插入数据,不需要指定字段列表: 要求插入的数据的值出现的顺序必须与表中设计的字段出现的顺序一致,凡是非数值数据,都需要使用引号(最好是单引号)包裹.insert into 表名 values(值列表1),(值列表2)...;      -- 可同时插入多个值列表mysql> insert into student values(1,'itheima001','张三','男'),(2,'itheima002','李四','女');Query OK, 2 rows affected (0.09 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from student;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    1 | itheima001 | 张三 | 男   ||    2 | itheima002 | 李四 | 女   ||    2 | itheima001 | 张三 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+4 rows in set (0.00 sec)方案2: 给部分字段插入数据,需要选定字段列表: 字段列表出现的顺序与字段的顺序无关,但是值列表的顺序必须与选定的字段的一致.insert into 表名 (字段列表) values (值列表1),(值列表2)...;例如:insert into student (name,sex,number,id) values ('张三','男','itheima001',2),('李四','男','itheima002',1); mysql> insert into student (name,sex,number,id) values ('张三','男','itheima001',2),('李四','男','itheima002',1);Query OK, 2 rows affected (0.07 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from student;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    1 | itheima001 | 张三 | 男   ||    2 | itheima002 | 李四 | 女   ||    2 | itheima001 | 张三 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+4 rows in set (0.00 sec)

9.查看数据

查看所有数据:select */字段列表 from 表名 [where条件];mysql> select * from student;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    1 | itheima001 | 张三 | 男   ||    2 | itheima002 | 李四 | 女   ||    2 | itheima001 | 张三 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+4 rows in set (0.00 sec)查看指定字段,指定条件的数据:select id,number,sex,name from student where id = 1;        -- 查看满足id为1的学生信息mysql> select id,number,sex,name from student where id=1;+------+------------+------+------+| id   | number     | sex  | name |+------+------------+------+------+|    1 | itheima001 | 男   | 张三 ||    1 | itheima002 | 男   | 李四 |+------+------------+------+------+2 rows in set (0.00 sec)

10.更新数据

updata 表名 set 字段 = 值 [where条件]; -- 建议都有where,如果不是更新全部updata student set sex = '男' where name = '张三';mysql> select * from student;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    1 | itheima001 | 张三 | 男   ||    2 | itheima002 | 李四 | 女   ||    2 | itheima001 | 张三 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+4 rows in set (0.00 sec)mysql> update student set sex = '男' where name = '李四';Query OK, 1 row affected (0.06 sec)Rows matched: 2  Changed: 1  Warnings: 0mysql> select * from student    -> ;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    1 | itheima001 | 张三 | 男   ||    2 | itheima002 | 李四 | 男   ||    2 | itheima001 | 张三 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+4 rows in set (0.00 sec)

11.删除数据

delete from 表名 [where条件];mysql> select * from student    -> ;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    1 | itheima001 | 张三 | 男   ||    2 | itheima002 | 李四 | 男   ||    2 | itheima001 | 张三 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+4 rows in set (0.00 sec)mysql> delete from student where name = '张三';Query OK, 2 rows affected (0.07 sec)mysql> select * from student;+------+------------+------+------+| id   | number     | name | sex  |+------+------------+------+------+|    2 | itheima002 | 李四 | 男   ||    1 | itheima002 | 李四 | 男   |+------+------------+------+------+2 rows in set (0.00 sec)

12.查看服务器能够识别的字符集

查看所有字符集:show character set;查看服务器默认对外处理的字符集:show variables like 'character_set%';mysql> show variables like 'character_set%';+--------------------------+---------------------------------------------------------------+| Variable_name            | Value                                                         |+--------------------------+---------------------------------------------------------------+| character_set_client     | gbk                                                           |    -- 服务器默认客户端来的数据的字符集| character_set_connection | gbk                                                           |    -- 连接层字符集| character_set_database   | gbk                                                           |    -- 当前所在数据库的字符集| character_set_filesystem | binary                                                        || character_set_results    | gbk                                                           |    -- 服务器默认的给外部数据的字符集| character_set_server     | latin1                                                        || character_set_system     | utf8                                                          || character_sets_dir       | C:\Program Files (x86)\MySQL\MySQL Server 5.7\share\charsets\ |+--------------------------+---------------------------------------------------------------+8 rows in set, 1 warning (0.01 sec)储存中文数据失败及乱码的原因及解决办法:原因: 由于客户端默认输出的字符集为gbk,而服务器默认来自客户端的数据字符集为utf8,gbk认为一个汉字有2个字节组成,而utf8认为一个汉字由3个字节组成,当服务器接受到来自客户端的字符集后发现不够读,因此转换汉字失败.由于客户端只识别GBK,而服务器默认给客户端的数据字符集为utf8,因此会产生乱码.解决办法: 将服务器认为的来自客户端的数据的字符集设置为GBKset character_set_client = gbk;将服务器给外部的数据的字符集设置为gbkset character_set_result = gbk;设置服务器对客户端的字符集的认识:   可使用快捷方式 set names 字符集;set names gbk;  ===>  character_set_client, character_set_connection,character_set_results

13.校对集问题

校对集:数据比较的方式校对集有3种格式:    _bin: binary,二进制比较,取出二进制位,一位一位的比较,区分大小写    _cs: case_sensitive,大小写敏感,区分大小写    _ci: case_insensitive,大小写不敏感,不区分大小写查看所有校对集:show collation;校对集:必须在有数据之前进行申明,如果已经有数据了再修改校对集,则修改无效.比较:根据某个字段排序:oder by 字段名 [asc|desc];   -- asc升序 , desc降序,默认为升序

14.如何解决乱码问题

浏览器,apache服务器(PHP),数据库服务器三码合一(统一编码)
0 0
原创粉丝点击