PHP技术栈

5、mysql整理

2017-09-22  本文已影响14人  回眸淡然笑

源文来自https://github.com/TIGERB/easy-tips,分享给大家 共同进步

常用sql语句

CREATE TABLE `table_name`(
  ...
  )ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `your_table_name`(`column_name`)
VALUES
('your_value_one'),
('your_value_two');
ALTER TABLE `your_table_name`
ADD `your_column_name` ...
AFTER `column_name`;
DELETE FORM `table_name`
WHERE ...;
TRUNCATE TABLE `your_table_name`;
DROP TABLE `your_table_name`;
ALTER TABLE `your_table_name`
DROP `column_name`;
ALTER TABLE `your_table_name`
DROP INDEX your_index_name(your_column_name);
UPDATE `table_name`
SET column_name=your_value
WHERE ...;
ALTER TABLE `your_table_name`
CHANGE `your_column_name` `your_column_name` ...(变更);
UPDATE `your_table_name`
AS a
JOIN `your_anther_table_name`
AS b
SET a.column = b.anther_column
WHERE a.id = b.a_id...;
SELECT `column_name_one`, `column_name_two`
FROM `table_name`;
SELECT *
FROM `your_table_name`
AS a
JOIN `your_anther_table_name`
AS b
WHERE a.column_name = b.column_name...;
SELECT aggregate_function(column_name)
FROM your_table_name
GROUP BY column_name
HAVING aggregate_function(column_name)...;
SELECT *
FROM database_name.your_table_name
AS a
JOIN another_database_name.your_another_table_name
AS b
WHERE a.column_name = b.column_name...;
  1. 复制一张表结构
CREATE TABLE `your_table_name`
LIKE `destination_table_name`;
  1. 完全复制一张表:表结构+全部数据
CREATE TABLE `your_table_name`
LIKE `destination_table_name`;

INSERT INTO `your_table_name`
SELECT *
FROM `destination_table_name`;

附录:mysql常用命令

mysql 5.7 新增用户

// 插入新用户
insert into mysql.user(Host, User, authentication_string, ssl_cipher, x509_issuer, x509_subject
value('localhost', 'username', password('password'), '', '', '');

// 数据库授权
grant all privileges on dbname.name.* to username@localhost identified by 'password';

// 刷新权限信息
FLUSH PRIVILEGES;
- 主键唯一索引
- 唯一索引
- 普通索引
- 联合索引
- 聚簇索引
- 非聚簇索引
- 乐观锁:
  update table table_name
  set column_name = value, version=version+1
  and
  where version = version;
- 悲观锁:update table table_name set column_name = value for update;
- 共享锁:x锁

- 脏读:一个事务内修改了数据,另一个事务读取并使用了这个数据;
- 幻读:一个事务内修改了涉及全表的数据,另一个事务往这个表里面插入了新的数据,第一个事务出现幻读;
- 不可重复读:一个事务内连续读了两次数据,中间另一个事务修改了这个数据,导致第一个事务前后两次读的数据不一致;
- 更新丢失:一个事务内变更了数据,另一个事务修改了这个数据,最后前一个事务commit导致另一个事务的变更丢失;
上一篇下一篇

猜你喜欢

热点阅读