鼎鼎知识库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10.Mysql的一些建表规则.md 5.6KB

пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. 选择数据库。
  2. ```
  3. 登录:mysql -u root -p
  4. 使用某个数据库:use classicmodels;
  5. 获取目前连接的数据库:select database();
  6. ```
  7. 创建数据库。
  8. - mysql -u root -p
  9. - show databases;
  10. - create database testdb;
  11. 删除数据库
  12. - DROP DATABASE testdb;
  13. 数字类型
  14. - TINTYINT
  15. - SMALLINT
  16. - MEDIUMINT
  17. - INT
  18. - BIGINT
  19. - DECIMAL
  20. - FLOAT
  21. - DOUBLE
  22. - BIT
  23. bool类型
  24. - TINYINT(1)
  25. 字符串类型
  26. - CHAR
  27. - VARCHAR
  28. - BINARY
  29. - VARBINARY
  30. - TINTYBOLOB
  31. - BLOG
  32. - MEDIUMBLOB
  33. - LONGBLOG
  34. - TINYTEXT
  35. - EXT
  36. - MEDIUMTEXT
  37. - LONGTEXT
  38. - ENUM
  39. - SET
  40. 时间类型
  41. - DATE
  42. - TIME
  43. - DATETIME
  44. - TIMESTAMP
  45. - YEAR
  46. 创建表简单表
  47. ```
  48. CREATE TABLE IF NOT EXISTS tasks (
  49. task_id INT AUTO_INCREMENT PRIMARY KEY,
  50. title VARCHAR(255) NOT NULL,
  51. start_date DATE,
  52. due_date DATE,
  53. status TINYINT NOT NULL,
  54. priority TINTYINT NOT NULL,
  55. description TEXT,
  56. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  57. ) ENGINE=INNODB;
  58. ```
  59. 创建表包含外键
  60. ```
  61. CREATE TABLE IF NOT EXISTS checklists (
  62. todo_id INT AUTO_INCREMENT,
  63. task_id INT,
  64. todo VARCHAR(255) NOT NULL,
  65. is_completed BOOLEAN NOT NULL DEFAULT FALSE,
  66. PRIMARY KEY (todo_id, task_id),
  67. FOREIGN KEY (task_id)
  68. REFERENCES tasks (task_id)
  69. ON UPDATE RESTRICT ON DELETE CASCADE
  70. )
  71. ```
  72. > 主键
  73. 主键是唯一的,不能为NULL。如果主键有多个,多个主键组合需要是唯一的。
  74. 只有一个主键。
  75. ```
  76. CREATE TABLE table_name (
  77. primary_key_column datatype PRIMARY KEY,
  78. )
  79. 或者
  80. CREATE TABLE table_name (
  81. priamry_key_column datatype,
  82. ...
  83. PRIMARY KEY(primary_key_column)
  84. )
  85. ```
  86. 多个主键。
  87. ```
  88. CREATE TABLE table_name (
  89. primary key_column1 datatype,
  90. priamry key_column2 datatype,
  91. ...
  92. PRIMARY KEY(key_column1, key_column2)
  93. )
  94. ```
  95. 实际例子。
  96. ```
  97. CREATE TABLE users(
  98. user_id INT AUTO_INCREMENT PRIMARY KEY,
  99. username VARCHAR(40),
  100. password VARCHAR(255),
  101. email VARCHAR(255)
  102. );
  103. CREATE TABLE roles(
  104. role_id INT AUTO_INCREMENT,
  105. role_name VARCHAR(50),
  106. PRIMARY KEY(role_id)
  107. )
  108. CREATE TABLE user_roles (
  109. user_id INT,
  110. role_id INT,
  111. PRIMARY KEY(user_id, role_id)
  112. FOREIGN KEY(user_id)
  113. REFERENCES users(user_id)
  114. FOREIGN KEY(role_id)
  115. REFERENCES roles(role_id)
  116. )
  117. ```
  118. 为一个表增加PRIMARY KEY
  119. ```
  120. CREATE TABLE pkdemos(
  121. id INT,
  122. title VARCHAR(255) NOT NULL
  123. )
  124. ALTER TABLE pkdemos
  125. ADD PRIMARY KEY(id);
  126. ```
  127. 增加唯一约束
  128. ```
  129. ALTER TABLE users
  130. ADD UNIQUE INDEX username_unique (username ASC)
  131. ALTER TABLE users
  132. ADD UNIQUE INDEX email_unique (email ASC)
  133. ```
  134. > 外键
  135. ```
  136. CREATE DATABASE fkdemo;
  137. USE fkdemo;
  138. CREATE TABLE categories(
  139. categoryId INT AUTO_INCREMENT PRIMARY KEY,
  140. categoryName VARCHAR(100) NOT NULL
  141. ) ENGINE=INNODB;
  142. CREATE TABLE products(
  143. productId INT AUTO_ICREMENT PRIMARY KEY,
  144. productName varchar(100) NOT NULL,
  145. categoryId INT,
  146. CONSTRAINT fk_category
  147. FOREIGN KEY(categoryId)
  148. REFERENCES categoreis(categoryId)
  149. ) ENGINE=INNODB;
  150. INSERT INTO categories(cateogryName) VALUES(''),('');
  151. SELECT * FROM categories;
  152. INSERT INTO products(productName, categoryId) VALUES ('i',1);
  153. INSERT INTO products(productName, categoryId) VALUES ('',3)
  154. Error Code:1452. Cannot add or update a child row: a forein key constrain fails
  155. UPDATE categories SET categoryId=100 WHERE categoryId=1;
  156. Error Code:1451. 因为categoryId被products表引用着,不能修改
  157. DROP TABLE produducts;
  158. CREATE TABLE products(
  159. productId INT AUTO_INCREMENT PRIMARY KEY,
  160. productName varchar(100) not null,
  161. categoryId INT NOT NULL,
  162. CONSTRAINT fk_category
  163. FOREIGN KEY(categoryId)
  164. REFERENCES categories(categoryId)
  165. ON UPDATE CASCASE
  166. ON DELETE CASCADE
  167. ) ENGINE=INNODB;
  168. INSERT INTO products(productName, categoryId)
  169. VALUES
  170. ('',1),
  171. ('',1),
  172. ('',2),
  173. ('',2);
  174. SELECT * FROM products;
  175. UPDATE categories
  176. SET categoryId=100
  177. WHERE categoryId=1;
  178. SELECT * FROM categories;
  179. SELECT * FROM products;
  180. DELETE FROM categories
  181. WHERE categoryId=2;
  182. SELECT * FROM categories;
  183. SELECT * FROM products;
  184. 接下来尝试可以设置为NULL
  185. DROP TABLE IF EXISTS categories;
  186. DROP TABLE IF EXISTS products;
  187. CREATE TABLE categories(
  188. categoryId INT AUTO_INCREMENT PRIMARY KEY,
  189. categoryName VARCHAR(100) NOT NULL
  190. )ENGINE=INNODB;
  191. CREATE TABLE products(
  192. productId INT AUTO_INCREMENT PRIMARY KEY,
  193. productName varchar(100) not null,
  194. categoryId INT,
  195. CONSTRAINT fk_category
  196. FORIEGN KEY(categoryId)
  197. REFERENCE categories(categoryId)
  198. ON UPDATE SET NULL 当外键表这个键没有的时候这里设置为null
  199. ON DELETE SET NULL
  200. )ENGINE=INNODB;
  201. INSERT INTO categories(categoryName)
  202. VALUES
  203. (''),
  204. ('')
  205. INSERT INTO products(productName, categoryId)
  206. VALUES
  207. ('iPhone', 1),
  208. ('Galaxy Note',1),
  209. ('Apple Watch',2),
  210. ('Samsung Galary Watch',2);
  211. UPDATE categories
  212. SET categoryId =100
  213. WHERE categoryId=1;
  214. SELECT * FROM categories;
  215. SELECT * FROM products; 发现categoryId为null
  216. 展示外键
  217. SHOW CREATE TABLE products;
  218. 删除外键
  219. ALTER TABLE products
  220. DROP FOREIGN KEY fk_category;
  221. 禁用外键
  222. SET foreign_key_checks=0;
  223. 启用外键
  224. SET foreign_key_checks=1;
  225. ```
  226. > 唯一约束
  227. ```
  228. CREATE TABLE suppliers (
  229. supplier_id INT AUTO_INCREMENT,
  230. name VARCHAR(255) NOT NULL,
  231. phone VARCHAR(15) NOT NULL UNIQUE,
  232. address VARCHAR(255) NOT NULL,
  233. PRIMARY KEY(supplier_id),
  234. CONSTRAINT uc_name_address UNIQUE (name, address)
  235. )
  236. 展示约束
  237. SHOW CREATE TABLE suppliers;
  238. 展示索引
  239. SHOW INDEX FROM suppliers;
  240. 删除约束
  241. DROP INDEX uc_name_address ON suppliers;
  242. 添加唯一约束
  243. ALTER TABLE supplers
  244. ADD CONSTRAINT uc_name_address
  245. UNIQUE(name, address)
  246. ```