当前位置:网站首页>常用sql语句整理:mysql
常用sql语句整理:mysql
2022-07-07 09:07:00 【全栈程序员站长】
## 常用sql语句整理:mysql
1. 增
– 增加一张表 “` 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`; “`
– 增加索引 + 主键 “` ALTER TABLE `your_table_name` ADD PRIMARY KEY your_index_name(your_column_name); “` + 唯一索引 “` ALTER TABLE `your_table_name` ADD UNIQUE your_index_name(your_column_name); “` + 普通索引 “` ALTER TABLE `your_table_name` ADD INDEX your_index_name(your_column_name); “` + 全文索引 “` ALTER TABLE `your_table_name` ADD FULLTEXT your_index_name(your_column_name); “`
2. 删
– 逐行删除 “` 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); “`
3. 改
– 变更数据 “` 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…; “`
4. 查
– 普通查询 “` 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…; “`
– 合计函数条件查询:WHERE 关键字无法与合计函数一起使用 “` 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…; “`
5. 复制一张表结构 “` CREATE TABLE `your_table_name` LIKE `destination_table_name`; “`
6. 完全复制一张表:表结构+全部数据 “` CREATE TABLE `your_table_name` LIKE `destination_table_name`;
INSERT INTO `your_table_name` SELECT * FROM `destination_table_name`; “`
—
### 附录:mysql常用命令 – 登陆: mysql -h host -u username -p – 列出数据库:SHOW DATABESES; – 列出表:SHOW TABLES; – 列出表结构:DESC table_name – 使用一个数据库:USE database_name; – 导入:source ‘file’; – 导出:mysqldump -h 127.0.0.1 -u root -p “database_name” “table_name” –where=”condition” > file_name.sql; – 查看慢日志:mysqldumpslow -s [c:按记录次数排序/t:时间/l:锁定时间/r:返回的记录数] -t [n:前n条数据] -g “正则” /path – 新增用户: insert into `user`(`Host`, `User`, `authentication_string`) value(‘localhost’, ‘username’, password(‘pwd’))
### 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 [email protected] identified by ‘password’;
// 刷新权限信息 FLUSH PRIVILEGES; “`
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113828.html原文链接:https://javaforall.cn
边栏推荐
- Shardingsphere sub database and table examples (logical table, real table, binding table, broadcast table, single table)
- What if copying is prohibited?
- Unity determines whether the mouse clicks on the UI
- Static semantic check of clang tidy in cicd
- PostgreSQL中的表复制
- Deep understanding of Apache Hudi asynchronous indexing mechanism
- What are the contents of the intermediate soft test, the software designer test, and the test outline?
- Online hard core tools
- 2022.7.4DAY596
- Which securities company is the best and safest to open an account for the subscription of new shares
猜你喜欢

Unity script generates configurable files and loads
![[untitled]](/img/f9/18b85ad17d4c560f2b9d95a53ee72a.jpg)
[untitled]
![[untitled]](/img/a0/29975bc0f9832e1640cc39dfce4a71.jpg)
[untitled]

【亲测可行】error while loading shared libraries的解决方案
![[untitled]](/img/c7/b6abe0e13e669278aea0113ca694e0.jpg)
[untitled]

July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
![[untitled]](/img/c2/d70d052b7e9587dc81c622f62f8566.jpg)
[untitled]

Idea shortcut keys

shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)

2021-04-08
随机推荐
Add a self incrementing sequence number to the antd table component
vim 的各种用法,很实用哦,都是本人是在工作中学习和总结的
Template initial level template
Input type= "password" how to solve the problem of password automatically brought in
【C#】WinForm运行缩放(变糊)的解决方法
2021-04-23
Cmake learning manual
Deeply understand the characteristics of database transaction isolation
PostgreSQL中的表复制
Introduction to shell programming
JS add spaces to the string
July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
China Southern Airlines pa3.1
uniCloud
2021 summary and 2022 outlook
seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)
Galaxy Kirin desktop operating system installation postgresql13 (source code installation)
[untitled]
verilog设计抢答器【附源码】
【pyqt】tableWidget里的cellWidget使用信号与槽机制