数据库基础2
引擎
mysql 从 5.5.5 开始 默认引擎 是 innodb
以前是 MyISAM
查看 引擎
show engines \G # mysql 以 分号为结尾 \G分行显示 最佳阅读体验
myisam、innodb 区别
- myisam不支持事务 表锁 支持全文索引 不支持外键 读取效率高 如果你这张表读的多那么选择 myisam 引擎 更好
- myisam只缓存索引文件 数据文件的缓存 由 操作系统来完成
- innodb 支持事务、行锁 、支持外键
指定表引擎
创建表的时候 mysql> create table test6(id int(11))engine=myisam;
alter table 表名 engine=innodb;
增删改查
插入记录
-
insert into 表名 values(值1,值2,值3,…值n);
INSERT into users VALUE('13999999999',8,'ruirui','yangyang','123456',19); #value只能插入一行 mysql> insert into users values('13888888888',1,'lijiarui','binggege','123abc',18); Query OK, 1 row affected (0.01 sec) #插入一条记录 values 支持插入多行记录 mysql> insert into users values('13888888888',2,'lijiarui','binggege','123abc',18),('13888888888',5,'lijiarui','binggege','123abc',18),('13888888888',3,'lijiarui','binggege','123abc',18),('13888888888',4,'lijiarui','binggege','123abc',18); #插入多条 Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0
-
insert into 表名(字段1,字段2,…,字段n) values(值1,值2,值3,…值n);
mysql> insert into users(id,user_name,password) value(13,'haha','heiheihei'); mysql> insert into users(id,user_name,password) values(9,'张三','123abc'),(10,'lisi','6666777'),(11,'wangwu','123123'),(12,'zhao si','9999120');
-
修改表字段 为主键自增 不用每次插入数据再插入主键了
mysql> alter table test1 modify id int(11) not null primary key auto_increment;
查询
select
诸如 python中的print
mysql> select '66666' as haha; # as 这里是起个别名
mysql> select '66666';
+-------+
| 66666 |
+-------+
| 66666 |
+-------+
1 row in set (0.00 sec)
mysql> select '66666' as haha;
+-------+
| haha |
+-------+
| 66666 |
+-------+
1 row in set (0.00 sec)
查询语句 语法
select * from 表名
select 字段1,字段2,…字段n from 表名;
select 字段1[as 别名],字段2,…字段n from 表名;
mysql> select * from users;
mysql> select id,user_name,age from users;
mysql> select id,user_name as 姓名,age as 年龄 from users;
单个字段不重复 distinct
mysql> select distinct user_name as 用户名 from users;
+----------+
| 用户名 |
+----------+
| binggege |
| yangyang |
| 张三 |
| lisi |
| wangwu |
| zhaosi |
| haha |
+----------+
7 rows in set (0.00 sec)
where 条件
符号 | 说明 |
---|---|
> | 大于 |
< | 小于 |
= | 等于 |
>= | 大于等于 |
<= | 小于等于 |
!= | 不等于 |
and | 并且 |
or | 或者 |
is | 是 |
is not | 不是 |
select * from users where id<=5;
select * from users where id<=8 and age>18; #同时满足 and 左右的条件才出来结果
select * from users where id<=8 or age>18; # 满足其中一个就返回
mysql> select * from users where jiaruilee is not null; #判断 jiaruilee 不为空的
mysql> select * from users where jiaruilee is null; # jiaruilee 为空的
like
mysql> select * from stars where username like '范冰冰&#