MySQL知识梳理
贴个大佬的数据库博客:http://blog.csdn.net/ayhan_huang/article/details/78790085
# MariaDB 的yum 源
# MariaDB 10.2 CentOS repository list - created 2017-12-07 02:59 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
# 安装命令
# sudo yum install MariaDB-server MariaDB-client
# 创建数据库,并指定字符集
create database web character set 'utf8';
# 创建表,并指定字符集
create table web_user ( username char(8), age tinyint(3), qq tinyint(20)) character set 'utf8';
# 表增加字段
alter table web_user add phone int(11);
# 修改表字段类型
alter table web_user modify phone int(32);
# 删除表字段
alter table web_user drop phone;
# 增加主键,并自增,限制int类型
alter table web_user add nid int primary key auto_increment;
# 删除主键
alter table web_user modify nid int(10); # 删除自增属性
alter table web_user drop primary key; # 删除主键
# 表插入数据,如果字段可以为空的话,前面的字段名可以选择性写几个,后面的value值需要对上
insert into web_user (name,age,qq,nid) values ('zhangsan',19,7964654,2);
# 表同时插入多条数据
insert into web_user (name,age,qq,nid) values ('zhangsan',19,7964654,2), ('zhangsan',19,7964654,2);
# 表修改数据,通过where指定修改的是那条数据.如果不指定where则修改表中全部条目
update web_user set name='wangwu' where nid=2;
# 表删除数据
delete from web_user where name='abc';
delete from web_user;
# 表查询数据
# 连表查询(以下全是)
select * from <table1>,<table2> where <table1.xx> = <table2.xx>; // 可以继续跟and 精确查
# 内连接
# <table1> inner join <table2> 相当于上面的语句 使用on来替换上面的 where 后面继续加 where来精确查
select * from <table1> inner join <table2> on <table1.xx> = <table2.xx>;
# 外连接
# <table1> left join <table2> 会以左边的表为主跟右边的表拼起来,如果右边的表没有跟左边的表对起来的就为Null.
# <table1> right join <table2> 跟 left 相反,以右边的表为准
# 一些MySQL小知识点:
# WHERE和HAVING:
# WHERE过滤行,HAVING过滤分组
# WHERE在数据分组前进行过滤, HAVING在数据分组后进行过滤,结合GROUP BY子句使用
# order by : 排序 desc 倒序 asc 升序
# limit : 限制查找的条数(可以在一定程度上提高查询性能)
# 一些MySQL聚合函数:
# AVG() 平均值
# COUNT() 统计行
# MAX() 最大值
# MIN() 最小值
# SUM() 求和
# MySQL索引建立, 以下 INDEX普通索引,UNIQUE唯一索引,PRIMARY KEY主键索引
# index_name为可选,缺省为第一个索引列名
ALTER TABLE <table_name> ADD INDEX <index_name> (column_list)
ALTER TABLE <table_name> ADD UNIQUE (column_list)
ALTER TABLE <table_name> ADD PRIMARY KEY (column_list)
# --- CREATE 方式建立索引,需要index_name 不可以建立主键索引
CREATE INDEX index_name ON table_name (column_list)
CREATE UNIQUE INDEX index_name ON table_name (column_list)
# --- 删除索引,删除掉table_name中的索引index_name
DROP INDEX index_name ON talbe_name
ALTER TABLE <table_name> DROP INDEX <index_name>
ALTER TABLE <table_name> DROP PRIMARY KEY
# Python3中Django使用MySQL数据库
# 应用__init__.py中导入 pymsql
import pymysql
pymysql.install_as_MySQLdb()
# settings.py 文件中
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'web',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '192.168.14.22',
'PORT': '3306',
}
}
# Django 管理已存在的数据库,执行下列命令,扫描数据库,会自动生成数据库的model
# 想使用就直接复制到 models.py 中
python manage.py inspectdb
# 注意自动生成model可能会有问题,比如数据库中int字段设置了最大长度,自动生成的可能没有
# 如果你想让django可以更改,删除,创建你的表,需要移除managed=False
# 自动生成的model名称可以随意命名,但是db_table的值不能更改,因为db_table的值是跟数据库的表对应的
# 自动生成的model示例:(很明显看出少了参数设置)
class WebUser(models.Model):
name = models.CharField(max_length=8, blank=True, null=True)
age = models.IntegerField(blank=True, null=True)
qq = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'web_user'
# 数据库中的表示例:
MariaDB [web]> desc web_user;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | char(8) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| qq | int(32) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MySQL引擎 MyISAM和InnoDB区别
innodb 不保存表具体的行数,也就是说当执行 count 计算的时候,innodb 是扫了一整遍的表,而MyISAM是直接读取保存的行数,如果count 中包含where 语句,那这两个引擎的操作是一样
DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除。
两种类型最主要的差别就是Innodb 支持事务处理与外键和行级锁。
浙公网安备 33010602011771号