第八章|MySQL数据库|库操作|表操作
Posted kris12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第八章|MySQL数据库|库操作|表操作相关的知识,希望对你有一定的参考价值。
1、初识数据库
我们在编写任何程序之前,都需要事先写好基于网络操作一台主机上文件的程序(socket服务端与客户端程序),于是有人将此类程序写成一个
专门的处理软件,这就是mysql等数据库管理软件的由来,但mysql解决的不仅仅是数据共享的问题,还有查询效率,安全性等一系列问题,
总之,把程序员从数据管理中解脱出来,专注于自己的程序逻辑的编写。
数据(Data):描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字、图片,图像、声音、语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机;在计算机中描述一个事物,就需要抽取这一事物的典型特征,组成一条记录,就相当于文件里的一行内容。
数据库(Databases,简称DB):数据库库即存放数据的仓库,只不过这个仓库是在计算机存储设备上,而且数据是按一定的格式存放的;数据库是长期存放在计算机内、有组织、可共享的数据即可。数据库中的数据按一定的数据模型组织、描述和储存,具有较小的冗余度、较高的数据独立性和易扩展性,并可为各种 用户共享。
数据库管理系统(DataBase Management System 简称DBMS)在了解了Data与DB的概念后,如何科学地组织和存储数据,如何高效获取和维护数据成了关键
这就用到了一个系统软件---数据库管理系统如MySQL、Oracle、SQLite、Access、MS SQL Server
常见的数据库模型分为关系型数据库(MySQL、Oracle、SQL Server....)和非关系型数据库(文档存储数据库MongoDB;键值存储数据库Redis、Memcached、列存储数据库HBase、图形数据库Neo4J)
mysql主要用于大型门户,例如搜狗、新浪等,它主要的优势就是开放源代码,因为开放源代码这个数据库是免费的,他现在是甲骨文公司的产品。
oracle主要用于银行、铁路、飞机场等。该数据库功能强大,软件费用高。也是甲骨文公司的产品。
sql server是微软公司的产品,主要应用于大中型企业,如联想、方正等。
数据库服务器-:运行数据库管理软件 数据库管理软件:管理-数据库 数据库:即文件夹,用来组织文件/表 表:即文件,用来存放多行内容/多条记录
1.1mysql的安装
mysql就是一个基于socket编写的C/S架构的软件
客户端软件
mysql自带:如mysql命令,mysqldump命令等
python模块:如pymysql
数据库管理软件分类--->>>
分两大类:
关系型:如sqllite,db2,oracle,access,sql server,MySQL,注意:sql语句通用
非关系型:mongodb,redis,memcache
可以简单的理解为:
关系型数据库需要有表结构
非关系型数据库是key-value存储的,没有表结构
mysql windows7的安装:https://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html?qq-pf-to=pcqq.c2c
关于如何清除mysql,很不好清除,一系列的注册表什么的:参考 https://blog.csdn.net/renwudao24/article/details/51860752
存储数据的仓库
mysql就是套接字服务端、客户端而已;
win+R 输入servers.msc 找到服务里边去启动mysql
select user(); #查看当前登录的账号 mysql> select user(); +----------------+ | user() | +----------------+ | ODBC@localhost | #代表本地账号 +----------------+ 1 row in set (0.10 sec)
C:\\Users\\Administrator>mysql -uroot -p #uroot就是管理员账号 Enter password: Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 9 .... mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
C:\\Users\\Administrator>mysqladmin -uroot -p password \'123\' #修改默认密码 Enter password: Warning: Using a password on the command line interface can be insecure. Warning: single quotes were not trimmed from the password by your command line client, as you might have expected.
C:\\Windows\\System32>tasklist | findstr mysql mysqld.exe 2032 Services 0 5,324 K C:\\Windows\\System32>taskkill /F /PID 2032 成功: 已终止 PID 为 2032 的进程。 C:\\Windows\\System32>net start MySQL MySQL 服务正在启动 .. MySQL 服务已经启动成功。
1.2修改mysql字符编码
统一字符编码
关于win7修改默认字符编码:https://blog.csdn.net/u013474104/article/details/52486880
建个my.ini配置文件
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It\'s a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] character-set-server=utf8 collation-server=utf8_general_ci # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = C:\\mysql-5.6.39-winx64 #这两个需要自行修改配置下path datadir = C:\\mysql-5.6.39-winx64\\data # port = ..... # server_id = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysql] default-character-set = utf8 [mysql.server] default-character-set = utf8 [mysqld_safe] default-character-set = utf8 [client] default-character-set = utf8
C:\\Windows\\system32>mysql Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 1 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> \\s -------------- mysql Ver 14.14 Distrib 5.6.39, for Win64 (x86_64) Connection id: 1 Current database: Current user: ODBC@localhost SSL: Not in use Using delimiter: ; Server version: 5.6.39 MySQL Community Server (GPL) Protocol version: 10 Connection: localhost via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: gbk Conn. characterset: gbk TCP port: 3306 Uptime: 18 sec Threads: 1 Questions: 5 Slow queries: 0 Opens: 67 Flush tables: 1 Open tabl es: 60 Queries per second avg: 0.277 -------------- mysql> show variables like \'character%\'; +--------------------------+----------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | C:\\mysql-5.6.39-winx64\\share\\charsets\\ | +--------------------------+----------------------------------------+ 8 rows in set (0.00 sec) mysql> exit Bye C:\\Windows\\system32>net stop MySQL MySQL 服务正在停止. MySQL 服务已成功停止。 C:\\Windows\\system32>net start MySQL MySQL 服务正在启动 . MySQL 服务已经启动成功。 C:\\Windows\\system32>mysql Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 1 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> \\s -------------- mysql Ver 14.14 Distrib 5.6.39, for Win64 (x86_64) Connection id: 1 Current database: Current user: ODBC@localhost SSL: Not in use Using delimiter: ; Server version: 5.6.39 MySQL Community Server (GPL) Protocol version: 10 Connection: localhost via TCP/IP Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 TCP port: 3306 Uptime: 13 sec Threads: 1 Questions: 5 Slow queries: 0 Opens: 67 Flush tables: 1 Open tabl es: 60 Queries per second avg: 0.384 -------------- mysql> show variables like \'character%\'; +--------------------------+----------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\\mysql-5.6.39-winx64\\share\\charsets\\ | +--------------------------+----------------------------------------+ 8 rows in set (0.00 sec)
1.3初识sql语句
跟数据有关的文件都放在data目录下
SQL语句:
操作文件夹(库)
操作数据库就是操作文件夹
增
create database db1 charset utf8;
查
show create database db1;
show databases; #查看所有的库
改
alter database db1 charset gbk;
删
drop database db1;
C:\\Users\\Administrator>mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 2 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> create database db1 charset utf8; Query OK, 1 row affected (0.06 sec) mysql> show create database db1; +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | db1 | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+--------------------------------------------------------------+ 1 row in set (0.06 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.14 sec) mysql> alter database db1 charset gbk; Query OK, 1 row affected (0.00 sec) mysql> show create database db1; +----------+-------------------------------------------------------------+ | Database | Create Database | +----------+-------------------------------------------------------------+ | db1 | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+-------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> drop database db1; Query OK, 0 rows affected (0.26 sec)
操作文件(表)
造表就是在硬盘上操作文件;.frm就是表标题之类的、.db就是表数据;
切换文件夹:use db1;
查看当前所在文件夹:select database();
增
create table t1(id int,name char);
查
show create table t1;
show tables;
desc t1;
改
alter table t1 modify name char(6);
alter table t1 change name NAME char(7);
删
drop table t1;
C:\\Users\\Administrator>mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 3 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> create database db1 charset utf8; Query OK, 1 row affected (0.00 sec) mysql> use db1; Database changed mysql> select database(); +------------+ | database() | +------------+ | db1 | +------------+ 1 row in set (0.00 sec) mysql> create table t1(id int,name char); Query OK, 0 rows affected (1.65 sec) mysql> show create table t1; +-------+------------------------------------------------------------------------------------------------------ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------- | t1 | CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL, `name` char(6) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+----------------------------------------------------------------------- 1 row in set (0.12 sec) mysql> show tables; +---------------+ | Tables_in_db1 | +---------------+ | t1 | +---------------+ 1 row in set (0.00 sec) mysql> desc t1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(6) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> alter table t1 change name NAME char(7); Query OK, 0 rows affected (1.31 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc t1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | NAME | char(7) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.05 sec) mysql> drop table t1; Query OK, 0 rows affected (0.23 sec) mysql> show tables; Empty set (0.00 sec)
操作文件内容(记录)
在表里边操作
增
insert t1(id,name) values(1,\'egon1\'),(2,\'egon2\'),(3,\'egon3\');
查
select id,name from db1.t1;
select * from db1.t1;(不推荐使用,测试的时候可以用)
改
update db1.t1 set name=\'SB\';
update db1.t1 set name=\'ALEX\' where id=2;
删
delete from t1;
delete from t1 where id=2;
mysql> use db1; Database changed mysql> insert t1(id,name) values(1,\'egon1\'),(2,\'egon2\'),(3,\'egon3\'); Query OK, 3 rows affected (0.53 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select id,name from db1.t1; +------+-------+ | id | name | +------+-------+ | 1 | egon1 | | 2 | egon2 | | 3 | egon3 | +------+-------+ 3 rows in set (0.00 sec) mysql> select id from db1.t1; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec) mysql> select * from db1.t1; +------+-------+ | id | name | +------+-------+ | 1 | egon1 | | 2 | egon2 | | 3 | egon3 | +------+-------+ 3 rows in set (0.00 sec) mysql> update db1.t1 set name=\'SB\'; Query OK, 3 rows affected (0.10 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> update db1.t1 set name=\'alex\' where id=2; Query OK, 1 row affected (0.16 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from db1.t1; +------+------+ | id | name | +------+------+ | 1 | SB | | 2 | alex | | 3 | SB | +------+------+ 3 rows in set (0.00 sec) mysql> delete from t1 where id=2; Query OK, 1 row affected (0.12 sec)
2、库操作
系统数据库
information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息、列信息、权限信息、字符信息等
performance_schema: MySQL 5.5开始新增一个数据库:主要用于收集数据库服务器性能参数,记录处理查询请求时发生的各种事件、锁等现象
mysql: 授权库,主要存储系统用户的权限信息
test: MySQL数据库系统自动创建的测试数据库
创建数据库
CREATE DATABASE 数据库名 charset utf8;
命名规则:可以由字母、数字、下划线、@、#、$;区分大小写;唯一性;不能使用关键字如 create select;不能单独使用数字;最长128位
查看数据库
show databases; ##所有的库
show create database db1;
select database(); ##当前选择的库
选择数据库
USE 数据库名;
删除数据库
DROP DATABASE 数据库名;
修改数据库
alter database db1 charset utf8;
3、表操作
可以用 help create help create database
3.1存储引擎
1、什么是存储引擎?
存储引擎就是表的类型
2、查看MySQL支持的存储引擎
show engines;
show engines\\G #查看所有支持的存储引擎
show variables like \'storage_engine%\'; #查看正在使用的存储引擎
3、指定表类型/存储引擎
create table t1(id int)engine=innodb;
create table t2(id int)engine=memory; #数据丢进去存到内存里边 #需要把mysql关了内存就清除了
create table t3(id int)engine=blackhole; #黑洞,数据丢进去就没了
create table t4(id int)engine=myisam; #.frm表结构;.MYD是它的表data文件;.MYI索引文件;
insert into t1 values(1);
insert into t2 values(1);
insert into t3 values(1);
insert into t4 values(1)
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.08 sec) mysql> show engines\\G *************************** 1. row *************************** Engine: FEDERATED Support: NO Comment: Federated MySQL storage engine Transactions: NULL XA: NULL Savepoints: NULL *************************** 2. row *************************** Engine: MRG_MYISAM Support: YES Comment: Collection of identical MyISAM tables Transactions: NO XA: NO Savepoints: NO *************************** 3. row *************************** Engine: MyISAM Support: YES Comment: MyISAM storage engine Transactions: NO XA: NO Savepoints: NO *************************** 4. row *************************** Engine: BLACKHOLE Support: YES Comment: /dev/null storage engine (anything you write to it disappears) Transactions: NO XA: NO Savepoints: NO *************************** 5. row *************************** Engine: CSV Support: YES Comment: CSV storage engine Transactions: NO XA: NO Savepoints: NO *************************** 6. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tables Transactions: NO XA: NO Savepoints: NO *************************** 7. row *************************** Engine: ARCHIVE Support: YES Comment: Archive storage engine Transactions: NO XA: NO Savepoints: NO *************************** 8. row *************************** Engine: InnoDB Support: DEFAULT Comment: Supports transactions, row-level locking, and foreign keys Transactions: YES XA: YES Savepoints: YES *************************** 9. row *************************** Engine: PERFORMANCE_SCHEMA Support: YES Comment: Performance Schema Transactions: NO XA: NO Savepoints: NO 9 rows in set (0.00 sec)
mysql>MySQL_第八章