当前位置:Gxlcms > 数据库问题 > MySQL SQL介绍(1)

MySQL SQL介绍(1)

时间:2021-07-01 10:21:17 帮助过:5人阅读

MySQL SQL介绍(1)

目录

  • MySQL SQL介绍(1)
    • 一、MySQL SQL介绍
      • 1.1.1 SQL 应用基础

一、MySQL SQL介绍

1.1.1 SQL 应用基础

  1. 常用的列属性约束
  1. <code>1、primary key (主键)
  2. 2、unique (唯一)
  3. 3、not null (不为空)
  4. 4、default (默认值)
  5. 5、auto_increment (自增长)
  6. 6、unsigned (无符号) 常与zerofill(零填充)配合
  7. 7、comment (注释)
  8. </code>
  1. 创库建表插入数据回顾
  1. <code>#创建school
  2. mysql> create database school character set utf8mb4 collate utf8mb4_general_ci;
  3. Query OK, 1 row affected (0.00 sec)
  4. mysql> Show collation;
  5. +--------------------------+----------+-----+---------+----------+---------+
  6. | Collation | Charset | Id | Default | Compiled | Sortlen |
  7. +--------------------------+----------+-----+---------+----------+---------+
  8. | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 |
  9. | big5_bin | big5 | 84 | | Yes | 1 |
  10. | dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 |
  11. | dec8_bin | dec8 | 69 | | Yes | 1 |
  12. | cp850_general_ci | cp850 | 4 | Yes | Yes | 1 |
  13. | cp850_bin | cp850 | 80 | | Yes | 1 |
  14. | hp8_english_ci | hp8 | 6 | Yes | Yes | 1 |
  15. | hp8_bin | hp8 | 72 | | Yes | 1 |
  16. | koi8r_general_ci | koi8r | 7 | Yes | Yes | 1 |
  17. mysql> use school
  18. Database changed
  19. #创建测试环境表
  20. #学生表
  21. mysql> create table student(
  22. -> sno int not null primary key auto_increment comment ‘学号‘,
  23. -> sname varchar(255) not null comment ‘学生姓名‘,
  24. -> sage tinyint(3) unsigned zerofill not null comment ‘学生年龄‘,
  25. -> ssex char(1) not null comment ‘学生性别‘
  26. -> )engine=innodb charset=utf8mb4;
  27. Query OK, 0 rows affected (0.00 sec)
  28. mysql> desc student;
  29. +-------+------------------------------+------+-----+---------+----------------+
  30. | Field | Type | Null | Key | Default | Extra |
  31. +-------+------------------------------+------+-----+---------+----------------+
  32. | sno | int(11) | NO | PRI | NULL | auto_increment |
  33. | sname | varchar(255) | NO | | NULL | |
  34. | sage | tinyint(3) unsigned zerofill | NO | | NULL | |
  35. | ssex | char(1) | NO | | NULL | |
  36. +-------+------------------------------+------+-----+---------+----------------+
  37. 4 rows in set (0.00 sec)
  38. #教师表
  39. mysql> create table teacher(
  40. -> tno int not null primary key auto_increment comment ‘教师编号‘,
  41. -> tname varchar(255) not null comment ‘教师名字‘
  42. -> )engine=innodb charset=utf8mb4;
  43. Query OK, 0 rows affected (0.04 sec)
  44. mysql> desc teacher;
  45. +-------+--------------+------+-----+---------+----------------+
  46. | Field | Type | Null | Key | Default | Extra |
  47. +-------+--------------+------+-----+---------+----------------+
  48. | tno | int(11) | NO | PRI | NULL | auto_increment |
  49. | tname | varchar(255) | NO | | NULL | |
  50. +-------+--------------+------+-----+---------+----------------+
  51. 2 rows in set (0.00 sec)
  52. #课程表
  53. mysql> create table course(
  54. -> cno int not null unique primary key auto_increment comment ‘课程编号‘,
  55. -> cname varchar(255) unique not null comment ‘课程名称‘,
  56. -> tno int unique not null comment ‘教师编号‘
  57. -> )engine=innodb charset=utf8mb4;
  58. Query OK, 0 rows affected (0.04 sec)
  59. mysql> desc course;
  60. +-------+--------------+------+-----+---------+----------------+
  61. | Field | Type | Null | Key | Default | Extra |
  62. +-------+--------------+------+-----+---------+----------------+
  63. | cno | int(11) | NO | PRI | NULL | auto_increment |
  64. | cname | varchar(255) | NO | UNI | NULL | |
  65. | tno | int(11) | NO | UNI | NULL | |
  66. +-------+--------------+------+-----+---------+----------------+
  67. 3 rows in set (0.00 sec)
  68. #成绩表
  69. mysql> create table score(
  70. -> sno int not null comment ‘学号‘,
  71. -> cno int not null comment ‘课程编号‘,
  72. -> score tinyint(3) unsigned zerofill not null comment ‘学生成绩‘
  73. -> )engine=innodb charset=utf8mb4;
  74. Query OK, 0 rows affected (0.05 sec)
  75. mysql> desc score;
  76. +-------+------------------------------+------+-----+---------+----------------+
  77. | Field | Type | Null | Key | Default | Extra |
  78. +-------+------------------------------+------+-----+---------+----------------+
  79. | sno | int(11) | NO | | NULL | auto_increment |
  80. | cno | int(11) | NO | | NULL | |
  81. | score | tinyint(3) unsigned zerofill | NO | | NULL | |
  82. +-------+------------------------------+------+-----+---------+----------------+
  83. 3 rows in set (0.00 sec)
  84. #学生表插入数据
  85. mysql> INSERT INTO student(sno,sname,sage,ssex)
  86. -> VALUES
  87. -> (1,‘zhang3‘,18,‘m‘),
  88. -> (2,‘zhang4‘,18,‘m‘),
  89. -> (3,‘li4‘,18,‘m‘),
  90. -> (4,‘wang5‘,19,‘f‘),
  91. -> (5,‘zh4‘,18,‘m‘),
  92. -> (6,‘zhao4‘,18,‘m‘),
  93. -> (7,‘ma6‘,19,‘f‘);
  94. Query OK, 7 rows affected (0.00 sec)
  95. Records: 7 Duplicates: 0 Warnings: 0
  96. mysql> select sno,sname,sage,ssex from student;
  97. +-----+--------+------+------+
  98. | sno | sname | sage | ssex |
  99. +-----+--------+------+------+
  100. | 1 | zhang3 | 018 | m |
  101. | 2 | zhang4 | 018 | m |
  102. | 3 | li4 | 018 | m |
  103. | 4 | wang5 | 019 | f |
  104. | 5 | zh4 | 018 | m |
  105. | 6 | zhao4 | 018 | m |
  106. | 7 | ma6 | 019 | f |
  107. +-----+--------+------+------+
  108. 7 rows in set (0.00 sec)
  109. mysql> INSERT INTO student(sname,sage,ssex)
  110. -> VALUES
  111. -> (‘oldboy‘,20,‘m‘),
  112. -> (‘oldgirl‘,20,‘f‘),
  113. -> (‘oldp‘,25,‘m‘);
  114. Query OK, 3 rows affected (0.00 sec)
  115. Records: 3 Duplicates: 0 Warnings: 0
  116. mysql> select sno,sname,sage,ssex from student;
  117. +-----+---------+------+------+
  118. | sno | sname | sage | ssex |
  119. +-----+---------+------+------+
  120. | 1 | zhang3 | 018 | m |
  121. | 2 | zhang4 | 018 | m |
  122. | 3 | li4 | 018 | m |
  123. | 4 | wang5 | 019 | f |
  124. | 5 | zh4 | 018 | m |
  125. | 6 | zhao4 | 018 | m |
  126. | 7 | ma6 | 019 | f |
  127. | 8 | oldboy | 020 | m |
  128. | 9 | oldgirl | 020 | f |
  129. | 10 | oldp | 025 | m |
  130. +-----+---------+------+------+
  131. 10 rows in set (0.00 sec)
  132. #教师表插入数据
  133. mysql> INSERT INTO teacher(tno,tname) VALUES
  134. -> (101,‘oldboy‘),
  135. -> (102,‘hesw‘),
  136. -> (103,‘oldguo‘);
  137. Query OK, 3 rows affected (0.00 sec)
  138. Records: 3 Duplicates: 0 Warnings: 0
  139. mysql> select tno,tname from teacher;
  140. +-----+--------+
  141. | tno | tname |
  142. +-----+--------+
  143. | 101 | oldboy |
  144. | 102 | hesw |
  145. | 103 | oldguo |
  146. +-----+--------+
  147. 3 rows in set (0.00 sec)
  148. #课程表插入数据
  149. mysql> INSERT INTO course(cno,cname,tno)
  150. -> VALUES
  151. -> (1001,‘linux‘,101),
  152. -> (1002,‘python‘,102),
  153. -> (1003,‘mysql‘,103);
  154. Query OK, 3 rows affected (0.00 sec)
  155. Records: 3 Duplicates: 0 Warnings: 0
  156. mysql> select cno,cname,tno from course;
  157. +------+--------+-----+
  158. | cno | cname | tno |
  159. +------+--------+-----+
  160. | 1001 | linux | 101 |
  161. | 1002 | python | 102 |
  162. | 1003 | mysql | 103 |
  163. +------+--------+-----+
  164. 3 rows in set (0.00 sec)
  165. #成绩表插入数据
  166. mysql> INSERT INTO score(sno,cno,score)
  167. -> VALUES
  168. -> (1,1001,80),
  169. -> (1,1002,59),
  170. -> (2,1002,90),
  171. -> (2,1003,100),
  172. -> (3,1001,99),
  173. -> (3,1003,40),
  174. -> (4,1001,79),
  175. -> (4,1002,61),
  176. -> (4,1003,99),
  177. -> (5,1003,40),
  178. -> (6,1001,89),
  179. -> (6,1003,77),
  180. -> (7,1001,67),
  181. -> (7,1003,82),
  182. -> (8,1001,70),
  183. -> (9,1003,80),
  184. -> (10,1003,96);
  185. Query OK, 17 rows affected (0.01 sec)
  186. Records: 17 Duplicates: 0 Warnings: 0
  187. mysql> select sno,cno,score from score;
  188. +-----+------+-------+
  189. | sno | cno | score |
  190. +-----+------+-------+
  191. | 1 | 1001 | 080 |
  192. | 1 | 1002 | 059 |
  193. | 2 | 1002 | 090 |
  194. | 2 | 1003 | 100 |
  195. | 3 | 1001 | 099 |
  196. | 3 | 1003 | 040 |
  197. | 4 | 1001 | 079 |
  198. | 4 | 1002 | 061 |
  199. | 4 | 1003 | 099 |
  200. | 5 | 1003 | 040 |
  201. | 6 | 1001 | 089 |
  202. | 6 | 1003 | 077 |
  203. | 7 | 1001 | 067 |
  204. | 7 | 1003 | 082 |
  205. | 8 | 1001 | 070 |
  206. | 9 | 1003 | 080 |
  207. | 10 | 1003 | 096 |
  208. +-----+------+-------+
  209. 17 rows in set (0.00 sec)
  210. </code>

MySQL SQL介绍(1)

标签:ica   field   创建   query   not   set   core   comm   ble   

人气教程排行