|
| 1 | +# SQL 笔记 |
| 2 | + |
| 3 | +## Windows 安装 MySQL |
| 4 | + |
| 5 | +如果是用 Mac 或者 Linux,强烈推荐用 Docker。如果是 Windows,那么可以考虑免安装的方式。 |
| 6 | + |
| 7 | +这里需要注意的是,安装分为2个部分: |
| 8 | +1. 将免安装 zip 包解压到适当的程序目录 |
| 9 | +2. 执行初始化 |
| 10 | +3. 安装 Service 方便开机自动启动 |
| 11 | + |
| 12 | +这种方式也符合“干净,绿色,无污染”的程序使用方式。尽量不要用 exe/msi 的形式安装。 |
| 13 | + |
| 14 | +```sh |
| 15 | +"D:\Program Files\MySQL Server 9\bin\mysqld.exe" --install MySQL9 --defaults-file="D:\Program Files\MySQL Server 9\my.ini" |
| 16 | + |
| 17 | +"D:\Program Files\MySQL Server 9\bin\mysqld" --defaults-file="D:\Program Files\MySQL Server 9\my.ini" --initialize --console |
| 18 | + |
| 19 | +C:\Users\aac>"D:\Program Files\MySQL Server 9\bin\mysqld" --defaults-file="D:\Program Files\MySQL Server 9\my.ini" --initialize --console |
| 20 | +2025-08-13T05:52:27.658938Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start. |
| 21 | +2025-08-13T05:52:27.663765Z 0 [System] [MY-013169] [Server] D:\Program Files\MySQL Server 9\bin\mysqld (mysqld 9.4.0) initializing of server in progress as process 38684 |
| 22 | +2025-08-13T05:52:27.682420Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. |
| 23 | +2025-08-13T05:52:27.879502Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. |
| 24 | +2025-08-13T05:52:28.800999Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: y<F+-t&(O2!Z |
| 25 | +2025-08-13T05:52:30.319063Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end. |
| 26 | + |
| 27 | + |
| 28 | +"D:\Program Files\MySQL Server 9\bin\mysqld" --console |
| 29 | +``` |
| 30 | +
|
| 31 | +## 1.查看数据库的一些设置: |
| 32 | +
|
| 33 | +```sh |
| 34 | +SHOW VARIABLES WHERE Variable_Name LIKE "%dir" |
| 35 | +``` |
| 36 | +
|
| 37 | +## 2.mysqldump 参数问题 |
| 38 | +``` |
| 39 | +mysqldump -uroot -proot -h 172.16.1.1 --master-data --lock-all-tables --net_buffer_length 20000 --default-character-set=utf8 db_name > init.sql |
| 40 | +``` |
| 41 | +
|
| 42 | +net_buffer_length 这个参数可以让一行数据不至于太大。否则读入的时候会有麻烦。 |
| 43 | +
|
| 44 | +master-data 可以加上主数据库的bin位置。 |
| 45 | +
|
| 46 | +
|
| 47 | +## 3. 修改密码 |
| 48 | +```sql |
| 49 | +set password for root@localhost = password('123'); |
| 50 | +或者 |
| 51 | +update mysql.user set password=password('123') where user='root' and host='localhost'; |
| 52 | +``` |
| 53 | +
|
| 54 | +## 查看数据库大小 |
| 55 | +
|
| 56 | +1.查看所有数据库容量大小 |
| 57 | +
|
| 58 | +```sql |
| 59 | +SELECT |
| 60 | + table_schema AS '数据库', |
| 61 | + sum( table_rows ) AS '记录数', |
| 62 | + sum( |
| 63 | + TRUNCATE ( data_length / 1024 / 1024, 2 )) AS '数据容量(MB)', |
| 64 | + sum( |
| 65 | + TRUNCATE ( index_length / 1024 / 1024, 2 )) AS '索引容量(MB)' |
| 66 | +FROM |
| 67 | + information_schema.TABLES |
| 68 | +GROUP BY |
| 69 | + table_schema |
| 70 | +ORDER BY |
| 71 | + sum( data_length ) DESC, |
| 72 | + sum( index_length ) DESC; |
| 73 | +``` |
| 74 | + |
| 75 | +
|
| 76 | +2.查看所有数据库各表容量大小 |
| 77 | +```sql |
| 78 | +SELECT |
| 79 | + table_schema AS '数据库', |
| 80 | + table_name AS '表名', |
| 81 | + table_rows AS '记录数', |
| 82 | + TRUNCATE ( data_length / 1024 / 1024, 2 ) AS '数据容量(MB)', |
| 83 | + TRUNCATE ( index_length / 1024 / 1024, 2 ) AS '索引容量(MB)' |
| 84 | +FROM |
| 85 | + information_schema.TABLES |
| 86 | +ORDER BY |
| 87 | + data_length DESC, |
| 88 | + index_length DESC; |
| 89 | +``` |
| 90 | +
|
| 91 | +3.查看指定数据库容量大小 |
| 92 | +例:查看mysql库容量大小 |
| 93 | +
|
| 94 | +```sql |
| 95 | +SELECT |
| 96 | + table_schema AS '数据库', |
| 97 | + sum( table_rows ) AS '记录数', |
| 98 | + sum( |
| 99 | + TRUNCATE ( data_length / 1024 / 1024, 2 )) AS '数据容量(MB)', |
| 100 | + sum( |
| 101 | + TRUNCATE ( index_length / 1024 / 1024, 2 )) AS '索引容量(MB)' |
| 102 | +FROM |
| 103 | + information_schema.TABLES |
| 104 | +WHERE |
| 105 | + table_schema = 'mysql'; |
| 106 | +``` |
| 107 | +
|
| 108 | +4.查看指定数据库各表容量大小 |
| 109 | +例:查看mysql库各表容量大小 |
| 110 | +
|
| 111 | +```sql |
| 112 | +SELECT |
| 113 | + table_schema AS '数据库', |
| 114 | + table_name AS '表名', |
| 115 | + table_rows AS '记录数', |
| 116 | + TRUNCATE ( data_length / 1024 / 1024, 2 ) AS '数据容量(MB)', |
| 117 | + TRUNCATE ( index_length / 1024 / 1024, 2 ) AS '索引容量(MB)' |
| 118 | +FROM |
| 119 | + information_schema.TABLES |
| 120 | +WHERE |
| 121 | + table_schema = 'mysql' |
| 122 | +ORDER BY |
| 123 | + data_length DESC, |
| 124 | + index_length DESC; |
| 125 | +``` |
| 126 | +
|
| 127 | +
|
| 128 | +# Mysql Workbench |
| 129 | +
|
| 130 | +在使用数据库建模的时候,我只用过 Mysql Workbench 和 SAP 的 PowerDesigner。这2个工具都不好用,不过由于考虑到跨平台和授权的因素,我还是优先选择了 MW(Mysql Workbench)。 |
| 131 | +
|
| 132 | +以下是使用的一些笔记: |
| 133 | +
|
| 134 | +## 关系连接线的标识 |
| 135 | +
|
| 136 | +分别是: |
| 137 | +
|
| 138 | +1. 双平行线:表示1个且一定要有一个 |
| 139 | +2. 单线条+小圆圈:表示一个或0个。 |
| 140 | +3. 线条+三分叉:表示多个,至少有1个。 |
| 141 | +4. 小圆圈+三分叉:表示多个,可以是0个。 |
| 142 | +
|
| 143 | +其中一对多或者一对一是由 Cardinality 这个选项决定的(在关系线上面双击就能修改。) |
| 144 | +
|
| 145 | +是否允许为0个是由 Mandatory 这个选项决定的。 |
| 146 | +
|
| 147 | +线条是实线还是虚线是由 Identifying Relationship 来决定的。 |
| 148 | +
|
| 149 | +但是实际在导出成为建表语句的时候,可以选择不将外键约束导出。 |
0 commit comments