目录
Mac安装Ubuntu启动Mysql
Mac安装Mysql
CenterOS
Mac
Mysql 8和5的密码插件区别
介绍
基础命令操作数据库
远程连接
数据库操作命令
表操作命令
数据操作命令
备份与恢复命令
查询分配
条件查询
聚合查询
分组查询
排序
分页查询
高级特性
表关系建立和外键
inner join 连接查询
完整的select语句
自关联
视图sql语句的封装
事务
索引
小例子
Python3.6数据库操作Demo
配置PYCharm
逻辑代码
面向对象封装
登录Demo
1.先安装VirtualBoxVM 安装搜狗
2.然后安装Ubuntu系统安装
3.打开Ubuntu左侧工具栏选择软件中心,搜索Mysql更新安装Mysql客户端和Mysql服务端源
4.执行命令安装
安装
sudo apt-get install mysql-server mysql-client
然后按照提示输入
一旦安装完成,MySQL 服务器应该自动启动。您可以在终端提示符后运行以下命令来检查 MySQL 服务器是否正在运行
sudo netstat -tap | grep mysql
出现如下代表已启动
deftmikejing@deftmikejing-VirtualBox:~$ sudo netstat -tap | grep mysql
[sudo] password for deftmikejing:
tcp6 0 0 [::]:mysql [::]:* LISTEN 1078/mysqld
deftmikejing@deftmikejing-VirtualBox:~$
或者
deftmikejing@deftmikejing-VirtualBox:/lib/systemd/system$ ps ajx|grep mysql
1 686 686 686 ? -1 Ss 120 0:00 /bin/sh /usr/bin/mysqld_safe
686 1141 686 686 ? -1 Sl 120 0:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
2877 6341 6340 2877 pts/1 6340 S+ 1000 0:00 grep --color=auto mysql
service mysql start
service mysql stop
service mysql restart
如果服务器不能正常运行,您可以通过下列命令启动它:
sudo /etc/init.d/mysql restart
mysql 的默认密码是linux系统密码,如果需要修改
mysql -u root -p
输入密码
set password =password('需要替换的密码');
5.设置mysql远程访问
使用 sudo netstat -lntp 命令看看
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
如果上面0 0.0.0.0后面不是3306的话就打开/etc/mysql/my.cnf 吧下面这一行给注释掉
bind-address = 127.0.0.1
修改配置以后记得要重启mysql
编辑mysql配置文件,把其中bind-address = 127.0.0.1注释了
vi /etc/mysql/mysql.conf.d/mysqld.cnf
建立远程连接
使用root进入mysql命令行
mysql -uroot -p
执行如下2个命令,示例中mysql的root账号密码:root(这root填写具体的密码)
grant all privileges on *.* to root@"%" identified by "password" with grant option;
flush privileges;
重启mysql
/etc/init.d/mysql restart
注意说明
本机登陆mysql:mysql -u root -p (-p一定要有);
从所有主机:grant all privileges on *.* to root@"%" identified by "password" with grant option;
从指定主机:grant all privileges on *.* to root@"192.168.11.205" identified by "password" with grant option;
例如:
grant all privileges on *.* to test@'192.168.113.124' identified by '123456'
然后执行
flush privileges;
重启Mysql
查看下刚才的账号
select host,user,password from mysql.user
到这里就可以测试下宿主机器ping通宿主下的Ubuntu
默认情况下无法ping通,需要修改网络设置 参考链接
之后下载Navicat用Mac链接Ubuntu里面的Mysql就可以用图形化工具操作了
一般我们部署到Linux上去,这里就先介绍些CenterOS的安装方法
以 MySQL 5.6 的 Yum 源为例,如果需要更高版本可以另寻,安装命令如下:
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install -y mysql mysql-server
运行如上命令即可完成安装,初始密码为空。接下来需要启动 MySQL 服务。
启动 MySQL 服务命令:
sudo systemctl start mysqld
停止、重启命令:
sudo systemctl stop mysqld
sudo systemctl restart mysqld
以上我们就完成了 Linux 下 MySQL 的安装,安装完成之后可以修改密码,可以执行如下命令:
mysql -uroot -p
输入密码后进入 MySQL 命令行模式。
use mysql;
UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
FLUSH PRIVILEGES;
命令中 newpass 即为修改的新的 MySQL 密码,请自行替换。
由于 Linux 一般会作为服务器使用,为了使得 MySQL 可以被远程访问,我们需要修改 MySQL 的配置文件,配置文件路径一般为 /etc/mysql/my.cnf。
如使用 vi 进行修改的命令如下:
vi /etc/mysql/my.cnf
取消此行的注释:
bind-address = 127.0.0.1
此行限制了 MySQL 只能本地访问而不能远程访问,取消注释即可解除此限制。
修改完成之后重启 MySQL 服务,这样 MySQL 就可以被远程访问了。
到此为止,Linux 下安装 MySQL 的过程结束。
推荐使用 Homebrew 安装,执行 brew 命令即可。
brew install mysql
启动、停止、重启 MySQL 服务的命令:
sudo mysql.server start
sudo mysql.server stop
sudo mysql.server restart
Mac 一般不会作为服务器使用,如果要想取消本地 host 绑定,同样修改 my.cnf 文件,然后重启服务即可。
这里如果找不到my.cnf文件,可以通过如下命令进行查找:
find 文件路径 参数
比如你可以通过以下命令在用户文件夹中搜索名字中包含screen的文件
find ~ -iname "screen*"
你也可以在特定的文件夹中寻找特定的文件,比如
find ~/Library/ -iname "com.apple.syncedpreferences.plist"
这个命令可以在Library文件夹中寻找com.apple.syncedpreferences.plist文件
mdfind命令就是Spotlight功能的终端界面,这意味着如果Spotlight被禁用,mdfind命令也将无法工作。mdfind命令非常迅速、高效。最基本的使用方法是:
mdfind -name 文件名字
比如你可以通过下面的命令寻找Photo 1.PNG文件
mdfind -name "Photo 1.PNG"
因为mdfind就是Spotlight功能的终端界面,你还可以使用mdfind寻找文件和文件夹的内容,比如通过以下命令寻找所有包含Will Pearson文字的文件:
mdfind "Will Pearson"
mdfind命令还可以通过-onlyin参数搜索特定文件夹的内容,比如
mdfind -onlyin ~/Library plist
这条命令可以搜索Library文件夹中所有plist文件。
mikejing$ mdfind -name "my.cnf"
/usr/local/etc/my.cnf
修改该文件下127.0.0.1 是否注释来调整是否可以远程连接。
安装之后进行配置
1.启动
mysql.server start
2.mysql配置脚本
/usr/local/opt/mysql/bin/mysql_secure_installation //mysql 提供的配置向导
详细如下
sunyichaodeMacBook-Pro:~ sunyichao$ /usr/local/opt/mysql/bin/mysql_secure_installation //mysql 提供的配置向导
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwordsand improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: k //是否采用mysql密码安全检测插件(这里作为演示选择否,密码检查插件要求密码复杂程度高,大小写字母+数字+字符等)
Please set the password for root here. // 首次使用自带配置脚本,设置root密码
New password:
Re-enter new password:
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n] Y //是否删除匿名用户
... Success!
Normally, root should only be allowed to connect from 'localhost'.This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y //是否禁止远程登录
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y //删除测试数据库,并登录
Dropping test database...
... Success!
Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y//重新载入权限表
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
sunyichaodeMacBook-Pro:~ sunyichao$
Navicat连接报错如下:
2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found
两个版本的区别
mysql> select version()
-> ;
+-----------+
| version() |
+-----------+
| 8.0.16 |
+-----------+
1 row in set (0.01 sec)
mysql> show variables like 'default_authentication_plugin'
-> ;
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.10 sec)
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
33 rows in set (0.01 sec)
mysql> select Host, User, Plugin from user;
+-----------+------------------+-----------------------+
| Host | User | Plugin |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.15-log |
+------------+
1 row in set (0.00 sec)
mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | mysql_native_password |
+-------------------------------+-----------------------+
1 row in set (0.01 sec)
mysql> select host,user,plugin from mysql.user;
+-----------+-----------+-----------------------+
| host | user | plugin |
+-----------+-----------+-----------------------+
| localhost | root | mysql_native_password |
| localhost | mysql.sys | mysql_native_password |
| % | root | mysql_native_password |
可以看到MySQL8.0.11版本默认的认证方式是caching_sha2_password ,而在MySQL5.7版本则为mysql_native_password。
若想在MySQL8.0版本中继续使用旧版本中的认证方式需要在my.cnf 文件中配置并重启,因为此参数不可动态修改。
mysql> set global default_authentication_plugin='mysql_native_password';
ERROR 1238 (HY000): Variable 'default_authentication_plugin' is a read only variable
写入my.cnf 文件后重启MySQL:
vim my.cnf
[mysqld]
default_authentication_plugin=mysql_native_password
上面的这种方案对已有的账户是无法修改的,只是后续新增的账户就会默认的Plugin变成mysql_native_password
第二种解决方法:兼容新老版本的认证方式。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户的密码
FLUSH PRIVILEGES; #刷新权限
创建新用户步骤,其中 with mysql_native_password不是必须的,默认根据配置文件走
--创建新的用户:
create user root@'%' identified WITH mysql_native_password BY 'password';
grant all privileges on *.* to root@'%' with grant option;
flush privileges;
--在MySQL8.0创建用户并授权的语句则不被支持:
mysql> grant all privileges on *.* to root@'%' identified by 'password' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'root' with grant option' at line 1
mysql>
E-R模型
三范式
字段类型
mysql -hip地址 -uroot -p
show databases;
create database 数据库名 charset=utf8;
drop database 数据库名;
use 数据库名;
select database();
show tables;
auto_increment表示自动增长
create table 表名(列及类型);
如:
create table students(
id int auto_increment primary key,
sname varchar(10) not null
);
修改表
alter table 表名 add|change|drop 列名 类型;
如:
alter table students add birthday datetime;
change举例
alter table 表名 change 原字段 修改后字段 类型
drop table 表名;
desc 表名;
rename table 原表名 to 新表名;
show create table 表名;
select * from 表名
全列插入:insert into 表名 values(...)
缺省插入:insert into 表名(列1,...) values(值1,...)
同时插入多条数据:insert into 表名 values(...),(...)...;
或insert into 表名(列1,...) values(值1,...),(值1,...)...;
update 表名 set 列1=值1,... where 条件
delete from 表名 where 条件
alter table students add isdelete bit default 0;
如果需要删除则
update students isdelete=1 where ...;
数据备份
sudo -s
Homebrew 安装目录 /usr/local/var/mysql/
cd /usr/local/var/mysql/
mysqldump –uroot –p 数据库名 > ~/Desktop/备份文件.sql;
按提示输入mysql的密码
数据恢复
连接mysql,创建数据库
退出连接,执行如下命令
mysql -uroot –p 数据库名 < ~/Desktop/备份文件.sql
根据提示输入mysql密码
select * from 表名;
消除重复行
select distinct gender from students;
select * from 表名 where 条件;
比较运算符
select * from students where id>3;
select * from subjects where id<=4;
select * from students where sname!='黄蓉';
select * from students where isdelete=0;
逻辑运算符
select * from students where id>3 and gender=0;
select * from students where id<4 or isdelete=0;
模糊查询
select * from students where sname like '黄%';
select * from students where sname like '黄_';
select * from students where sname like '黄%' or sname like '%靖%';
范围查询
select * from students where id in(1,3,8);
select * from students where id between 3 and 8;
select * from students where id between 3 and 8 and gender=1;
空判断
select * from students where hometown is null;
select * from students where hometown is not null;
select * from students where hometown is not null and gender=0;
优先级
select count(*) from students;
select max(id) from students where gender=0;
select min(id) from students where isdelete=0;
select sum(id) from students where gender=1;
select avg(id) from students where isdelete=0 and gender=0;
select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
select gender as 性别,count(*)
from students
group by gender;
select hometown as 家乡,count(*)
from students
group by hometown;
分组后的数据筛选
select 列1,列2,聚合... from 表名
group by 列1,列2,列3...
having 列1,...聚合...
方案一
select count(*)
from students
where gender=1;
-----------------------------------
方案二:
select gender as 性别,count(*)
from students
group by gender
having gender=1;
对比where与having
select * from 表名
order by 列1 asc|desc,列2 asc|desc,...
select * from students
where gender=1 and isdelete=0
order by id desc;
select * from subject
where isdelete=0
order by stitle;
select * from 表名
limit start,count
示例:分页
select * from students
where isdelete=0
limit (n-1)*m,m
案例一:
学生 1对多 成绩 学生表中一行能对应多行成绩出现
科目 1对多 成绩 科目中一行能对应多行成绩出现
成绩 1对1 学生和科目 一条成绩必定对应一个学生和一个科目
案例二:
班级 1对多 学生 班级表中一行,能对应多行学生出现
学生 1对1 班级 一个学生必定对应一个班级
1对多代表1的这一方会出现多个在另一方,因此主键关联都会写在学生表中或者成绩表中
这里学生,成绩,科目三个的分析和电商里面 用户,购物车,商品是一样的,首先
学生和科目是n:m的关系,无法维护,因此需要再开一张表成绩来维护好比用户和商品是n:m的关系,因此需要购物车来维护,用户和购物车是1:n,商品和购物车也是1:n
以下是科目和学生的标 以及创建一张成绩单的标,
+----+---------+
| id | title |
+----+---------+
| 1 | Science |
| 2 | Math |
| 3 | English |
| 4 | Pysical |
| 5 | Mamical |
+----+---------+
5 rows in set (0.00 sec)
mysql> select *from students;
+----+------------+--------+---------------------+----------+-------+----------+
| id | name | gender | birthday | isDelete | score | address |
+----+------------+--------+---------------------+----------+-------+----------+
| 1 | Mekejing | | 2018-01-01 00:00:00 | | 97 | NULL |
| 2 | jiaojiao | | 2018-01-01 00:00:00 | | 55 | NULL |
| 3 | elf | | 2018-01-01 00:00:00 | | 100 | shenzhen |
| 4 | 11111 | | 2018-01-01 00:00:00 | | 60 | shenzhen |
| 6 | 3333333 | | 2018-01-01 00:00:00 | | 77 | shenzhen |
| 7 | wuke | | 0000-00-00 00:00:00 | | 80 | NULL |
| 8 | Deft | | 2019-01-01 00:00:00 | | 32 | NULL |
| 9 | jiaoBo | | 2001-01-01 00:00:00 | | 71 | NULL |
| 10 | oojiaojiao | | 2001-01-01 00:00:00 | | 71 | NULL |
+----+------------+--------+---------------------+----------+-------+----------+
mysql> create table score(
-> id int auto_increment primary key not null,
-> score decimal(4,1),
-> stuid int,
-> subid int,
-> foreign key(stuid) references students(id),
-> foreign key(subid) references subjects(id))
-> ;
Query OK, 0 rows affected (0.02 sec)
mysql> desc score;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| score | decimal(4,1) | YES | | NULL | |
| stuid | int(11) | YES | MUL | NULL | |
| subid | int(11) | YES | MUL | NULL | |
+-------+--------------+------+-----+---------+----------------+
查看原始的创建脚本
show create table score;
| score | CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`score` decimal(4,1) DEFAULT NULL,
`stuid` int(11) DEFAULT NULL,
`subid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `stuid` (`stuid`),
KEY `subid` (`subid`),
CONSTRAINT `score_ibfk_1` FOREIGN KEY (`stuid`) REFERENCES `students` (`id`),
CONSTRAINT `score_ibfk_2` FOREIGN KEY (`subid`) REFERENCES `subjects` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 |
可以看到stuid和subid都是关联着各自表中的primary key
可以和上面一样直接在创建表的时候创建外键约束,也可以alter添加
alter table scores add constraint stu_sco foreign key(stuid) references students(id);
外键的级联操作)
例如删除students里面的一个学生,那么score里面已经有数据关联上,如何处理?
alter table scores add constraint stu_sco foreign key(stuid) references students(id) on delete cascade;
删除约束
查看表建立语句
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| score | CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`score` decimal(5,2) DEFAULT NULL,
`stuid` int(11) DEFAULT NULL,
`subid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `subid` (`subid`),
KEY `stu_sco` (`stuid`),
CONSTRAINT `stu_sco` FOREIGN KEY (`stuid`) REFERENCES `students` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 |
删除约束
alter table score drop foreign key stu_sco;
这时候就可以删除级联的表数据了,比如这个时候你删除了students表的数据,其他表有外键关联也没事,因为约束已经解除了,但是这个时候你要添加约束,就会报错,因为数据已经找不到了
alter table score add constraint stu_sco foreign key(stuid) references students(id);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`mikejing`.`#sql-15650_18`, CONSTRAINT `stu_sco` FOREIGN KEY (`stuid`) REFERENCES `students` (`id`))
mysql> delete from score where id = 4;
Query OK, 1 row affected (0.00 sec)
mysql> alter table score add constraint stu_sco foreign key(stuid) references students(id);
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
select students.sname,subjects.stitle,scores.score
from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id;
select *from score inner join students;
这是最基本的,如果score表10条和students表5条,因此在不加on条件下,组合起来是10*5=50条新的数据组合,然后根据on后面的组合筛选,比如下面的操作,id关联到的,因此自关联的表可以理解为 自身表条目的平方组合出来的数据进行筛选即可
实际上就是多个表链接成一个大表
select students.sname,avg(scores.score)
from scores
inner join students on scores.stuid=students.id
group by students.sname;
select students.sname,avg(scores.score)
from scores
inner join students on scores.stuid=students.id
where students.gender=1
group by students.sname;
select subjects.stitle,avg(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
group by subjects.stitle;
select subjects.stitle,avg(scores.score),max(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
where subjects.isdelete=0
group by subjects.stitle;
select sum(anum) from (select students.name,sum(score.score) as anum from score inner join students on score.stuid=students.id where gender=1 group by students.name) as MKJ;
select distinct *
from 表名 inner |left|right|on join 表 on 关系
where ....
group by ... having ...
order by ...
limit star,count
以省市县为例,这些数据不需要开多个表,可以通过自关联的方式完成,例如
id title pid
1 浙江 NULL
2 宁波 1(关联到自身表中另一行)
3 慈溪 2 (关联到自身表中另一行)
。。。。。
这种设计还有类似分类,一张表中可以无限数据,无限分类下去,充分利用空间
create table areas(
id int primary key,
atitle varchar(20),
pid int,
foreign key(pid) references areas(id)
);
create view stuscore as
select students.*,scores.score from scores
inner join students on scores.stuid=students.id;
select * from stuscore;
数据被更改时 insert update delete
show create table students;
alter table '表名' engine=innodb;
开启begin;
提交commit;
回滚rollback;
事务可以理解为对同一个东西的修改操作,可以理解为OC里面的setter操作,多线程会抢资源,因此会有锁的存在,事务也是一样,通过锁来保证数据执行的一致性和隔离性,事务其实就通过锁在内存中开辟一个空间进行计算,通过commit或者rollback来确定是否需要采用之前的修改,如果其他线程也在操作数据库而且是操作同一条,就会出现锁等待,等commit或者rollback之后解锁继续执行
先做个浅显的理解,到时候深入了再来记录;
其实事务可以优化的点想到一个,比如你要插入很多条数据,直接执行insert语句不开启事务,其实每次都会对数据库的更改和提交操作,因此开启事务,再合理内存配置范围内,通过事务一次性提交更改物理数据库,减少损耗
事务引擎提交事务会开启行级锁
Mysql 索引原理
Mysql索引使用方式介绍
MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构。提取句子主干,就可以得到索引的本质:索引是数据结构。
我们知道,数据库查询是数据库的最主要功能之一。我们都希望查询数据的速度能尽可能的快,因此数据库系统的设计者会从查询算法的角度进行优化。最基本的查询算法当然是顺序查找(linear search),这种复杂度为O(n)的算法在数据量很大时显然是糟糕的,好在计算机科学的发展提供了很多更优秀的查找算法,例如二分查找(binary search)、二叉树查找(binary tree search)等。如果稍微分析一下会发现,每种查找算法都只能应用于特定的数据结构之上,例如二分查找要求被检索数据有序,而二叉树查找只能应用于二叉查找树上,但是数据本身的组织结构不可能完全满足各种数据结构(例如,理论上不可能同时将两列都按顺序进行组织),所以,在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法。这种数据结构,就是索引。
可以理解为数据本身的组织结构不可能满足各种数据结构,比如满足有序的二分查找或者二叉树查找,因此需要建立索引,把该字段或者多个字段下的所有数据都拉出来新建一个目录,满足数据结构查找,比如有序拉出来二分查找。
选择数据类型
普通索引
普通索引仅有一个功能:加速查询
创建表
create table in1(
nid int not null auto_increment primary key,
name varchar(32) not null,
email varchar(64) not null,
extra text,
index ix_name (name)
)
创建
create index index_name on table_name(column_name)
查看
show index from table_name;
删除
drop index_name on table_name;
注意:对于创建索引时如果是BLOB 和 TEXT 类型,必须指定length。
create index ix_extra on in1(extra(32));
缺点
虽然索引提高了查询速度,但是会降低更新表的速度,如果对表进行INSERT UPDATE和DELETE,Mysql不仅要更新数据,还有保存更新索引文件,而且建立索引会占磁盘索引对应的文件
- 查看执行时间
set profiling = 1;
SQL...
show profiles;
详细介绍可以看上面的文章
1、设计班级表,与学生表关联,并进行查询
2、设计分类表,自关联,并进行查询
3、创建视图存储上面的两个查询
# 班级表和学生表关联
1.班级表和学生表 班级表一条数据可以对应多条学生数据 1:n 因此关联外键设置在n这里
create table Class(
id int auto_increment primary key not null,
name varchar(10));
create table Student(
id int auto_increment primary key not null,
name varchar(10),
clsid int,
foreign key(clsid) references Class(id));
create view V_stu_class as
select Student.id,Student.name,Class.name as className
from Student
inner join Class on Student.clsid=Class.id
# 自关联
create table typeClass(
id int auto_increment primary key not null,
name varchar(10),
pid int,
foreign key(pid) references typeClass(id));
create view V_types as
select son.*
from typeClass as father
inner join TypeClass as son on son.pid=father.id
首先Python3x之后需要安装 pymysql
In [1]: import os
In [2]: os.__file__
Out[2]: '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py'
In [3]:
以上目录能找到自带的安装包,我们执行
pip3 install pymysql
可以在该目录下找到site-packages文件夹,里面有我们自行安装的所有安装包,我们在cmd下面import会在该目录下自动查找,OK
但是我们用PYCharm的时候,你import你自行安装的包,是会报错的,告诉我们找不到该moduel,由于上不了图了,说下步骤
1.打开PYCharm,File ------ Default Settings---------选择Project Interpreter,右侧会出现默认的配置环境
2.由于默认的环境不会加载site-packages,需要自己配置新的,点击齿轮,选择Add
3.这里的弹出框默认值不用管,只要把下面的Inherit global site-packages选上,第三方包就出现了
4.编译的时候右上角会出现对应的文件名选项,点击Edit Configuration选择对应的环境即可
以下是配置好之后的代码
import pymysql
config = {
'host':'192.168.0.61',
'port':3306,
'user':'root',
'password':'mikejing',
'db':'python',
'charset':'utf8'
}
# 链接打开数据库
db = pymysql.connect(**config)
# 使用cousor() 创建一个游标对象 cursor
cursor = db.cursor()
# 使用excute执行sql语句
cursor.execute("SELECT VERSION()")
# 使用fetchone获取单条数据
data = cursor.fetchone()
print("Database version:%s"%data)
# sql = "insert into students (name) values('握草')"
# sql = "update students set name='框要是' where id=12"
# sql = "delete from students where id=12"
# data = input("请输入姓名:")
# sql = "insert into students (name) values(%s)"
# cursor.execute(sql, data)
sql = "select * from students where id=13"
cursor.execute(sql)
info = cursor.fetchone()
print(info)
sql = "select * from students"
cursor.execute(sql)
info1 = cursor.fetchall()
print(info1)
try:
cursor.execute("show tables")
except Exception as result:
print(result)
print(cursor.fetchall())
name =input("请输入修改后的名字:")
ids = input("请输入修改用户ID:")
params = [name,ids]
sql = "update students set name=%s where id=%s"
cursor.execute(sql,params)
# 默认开启事务,需要提交
db.commit()
cursor.close()
# 关闭数据库
db.close()
对象
import pymysql
class MysqlHelper(object):
def __init__(self,host, port, user, password, db, charset='utf8'):
self.host = host
self.port = port
self.user = user
self.password = password
self.db = db
self.charset = charset
def connect(self):
self.con = pymysql.connect(host=self.host,port=self.port,user=self.user,password=self.password,db=self.db,charset=self.charset)
self.cursor = self.con.cursor()
def dealloc(self):
self.con.close()
self.cursor.close()
def cud(self, sql, params):
try:
self.connect()
self.cursor.execute(sql,params)
self.con.commit()
self.dealloc()
except Exception as result:
print(result)
def watch(self, sql, params=[]):
try:
self.connect()
self.cursor.execute(sql,params)
result = self.cursor.fetchall()
self.con.commit
self.dealloc()
return result
except Exception as result:
print("error:%s"%result)
使用
import MysqlHelper
helper = MysqlHelper.MysqlHelper('192.168.0.61', 3306, 'root', 'mikejing', 'python')
name = input("请输入修改后的名字:")
ids = input("请输入需要修改的用户id:")
params = (name, ids)
sql = "update students set name=%s where id=%s"
# 或者直接凭借
# sql = "update students set name=%s where id=%s"%(name, ids)
helper.cud(sql, params)
sql = "select * from students where id >20"
result = helper.watch(sql)
print(result)
/Users/mintou/venv/bin/python /Users/mintou/Desktop/PYCharm/tPython.py
请输入修改后的名字:29岁翻倍
请输入需要修改的用户id:30
((24, 'hehehe', b'\x00', datetime.datetime(1990, 1, 1, 0, 0), b'\x00', 100, 'cixi', 'www.ads.com'), (25, '我是神e', b'\x00', datetime.datetime(1990, 1, 1, 0, 0), b'\x00', 100, 'cixi', 'www.ads.com'), (26, 'iOS大神', b'\x00', datetime.datetime(1990, 1, 1, 0, 0), b'\x00', 100, 'cixi', 'www.ads.com'), (27, '宓珂璟', b'\x01', None, b'\x00', None, None, None), (28, 'python大神', b'\x01', None, b'\x00', None, None, None), (30, '29岁翻倍', b'\x01', None, b'\x00', None, None, None))
Process finished with exit code 0
还是用上面的MysqlHelper工具 用sha1试试
正常登录是RSA,用户在客户端输入密码,通过公钥加密给后台,后台私钥解密,加盐之后然后AES,接着存入数据库,就是注册
下次登录的时候,输入用户名密码,输入密码,公钥加密,后台私钥解密,然后根据用户名去数据库select,匹配上就登录成功
import MysqlHelper
import hashlib
helper = MysqlHelper.MysqlHelper('192.168.0.61', 3306, 'root', 'mikejing', 'python')
def sha1Operation(pwd):
hash_pwd = pwd.encode("utf-8")
hashTool = hashlib.sha1()
hashTool.update(hash_pwd)
return hashTool.hexdigest()
print("注册用户密码")
name = input("name:")
pwd = input("pwd:")
parmes = (name,sha1Operation(pwd))
sql = "insert into userinofs values(0,%s,%s)"
helper.cud(sql, parmes)
print("*"*50+"注册成功" + "*"*50)
while True:
print("登录")
name = input("name:")
pwd = input("pwd:")
parames = (name)
sql = "select pwd from userinofs where username = %s"
result = helper.watch(sql, parames)
if len(result) == 0:
print("用户名不存在")
elif result[0][0] == sha1Operation(pwd):
print("登录成功")
break;
else:
print("密码错误")
参考文章
Python3各种模块安装(比如Mysql)
Mysql安装初始化
find命令