figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; }
button, input, select, textarea { color: inherit; font: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 2; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.7); color: rgb(85, 85, 85); border-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px !important; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right: 0px; background-color: inherit; }
.CodeMirror-linenumber { user-select: none; }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background: inherit; position: relative !important; }
.md-diagram-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; background: 0px 0px; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; -webkit-tap-highlight-color: transparent; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print {
body, html { border: 1px solid transparent; height: 99%; break-after: avoid; break-before: avoid; }
#write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
.typora-export * { -webkit-print-color-adjust: exact; }
html.blink-to-pdf { font-size: 13px; }
.typora-export #write { padding-left: 32px; padding-right: 32px; padding-bottom: 0px; break-after: avoid; }
.typora-export #write::after { height: 0px; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background: rgb(204, 204, 204); display: block; overflow-x: hidden; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
[contenteditable="true"]:active, [contenteditable="true"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) {
.md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
.md-toc-h4 .md-toc-inner { margin-left: 5em; }
.md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
.md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; vertical-align: initial; }
a.md-print-anchor { white-space: pre !important; border-width: initial !important; border-style: none !important; border-color: initial !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; background: 0px 0px !important; text-decoration: initial !important; text-shadow: initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: hidden; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; zoom: 90%; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; overflow-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; }
.MathJax_SVG * { transition: none 0s ease 0s; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; margin-top: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="mermaid"] svg, [lang="flow"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }

.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background: inherit; }
.CodeMirror-scroll { overflow: auto hidden; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right: 1px solid rgb(221, 221, 221); background: inherit; white-space: nowrap; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background: inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background: inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; background: 0px 0px !important; border: none !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-radius: 0px; border-width: 0px; background: 0px 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; overflow-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; }
.CodeMirror-wrap pre { overflow-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right: 30px solid transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background: rgba(255, 255, 0, 0.4); }
@media print {
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}

:root { --side-bar-bg-color: #fafafa; --control-text-color: #777; }
html { font-size: 16px; }
body { font-family: "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); line-height: 1.6; }
#write { max-width: 860px; margin: 0px auto; padding: 30px 30px 100px; }
#write > ul:first-child, #write > ol:first-child { margin-top: 30px; }
a { color: rgb(65, 131, 196); }
h1, h2, h3, h4, h5, h6 { position: relative; margin-top: 1rem; margin-bottom: 1rem; font-weight: bold; line-height: 1.4; cursor: text; }
h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor { text-decoration: none; }
h1 tt, h1 code { font-size: inherit; }
h2 tt, h2 code { font-size: inherit; }
h3 tt, h3 code { font-size: inherit; }
h4 tt, h4 code { font-size: inherit; }
h5 tt, h5 code { font-size: inherit; }
h6 tt, h6 code { font-size: inherit; }
h1 { padding-bottom: 0.3em; font-size: 2.25em; line-height: 1.2; border-bottom: 1px solid rgb(238, 238, 238); }
h2 { padding-bottom: 0.3em; font-size: 1.75em; line-height: 1.225; border-bottom: 1px solid rgb(238, 238, 238); }
h3 { font-size: 1.5em; line-height: 1.43; }
h4 { font-size: 1.25em; }
h5 { font-size: 1em; }
h6 { font-size: 1em; color: rgb(119, 119, 119); }
p, blockquote, ul, ol, dl, table { margin: 0.8em 0px; }
li > ol, li > ul { margin: 0px; }
hr { height: 2px; padding: 0px; margin: 16px 0px; background-color: rgb(231, 231, 231); border: 0px none; overflow: hidden; box-sizing: content-box; }
li p.first { display: inline-block; }
ul, ol { padding-left: 30px; }
ul:first-child, ol:first-child { margin-top: 0px; }
ul:last-child, ol:last-child { margin-bottom: 0px; }
blockquote { border-left: 4px solid rgb(223, 226, 229); padding: 0px 15px; color: rgb(119, 119, 119); }
blockquote blockquote { padding-right: 0px; }
table { padding: 0px; word-break: initial; }
table tr { border-top: 1px solid rgb(223, 226, 229); margin: 0px; padding: 0px; }
table tr:nth-child(2n), thead { background-color: rgb(248, 248, 248); }
table tr th { font-weight: bold; border-width: 1px 1px 0px; border-top-style: solid; border-right-style: solid; border-left-style: solid; border-top-color: rgb(223, 226, 229); border-right-color: rgb(223, 226, 229); border-left-color: rgb(223, 226, 229); border-image: initial; border-bottom-style: initial; border-bottom-color: initial; margin: 0px; padding: 6px 13px; }
table tr td { border: 1px solid rgb(223, 226, 229); margin: 0px; padding: 6px 13px; }
table tr th:first-child, table tr td:first-child { margin-top: 0px; }
table tr th:last-child, table tr td:last-child { margin-bottom: 0px; }
.CodeMirror-lines { padding-left: 4px; }
.code-tooltip { box-shadow: rgba(0, 28, 36, 0.3) 0px 1px 1px 0px; border-top: 1px solid rgb(238, 242, 242); }
.md-fences, code, tt { border: 1px solid rgb(231, 234, 237); background-color: rgb(248, 248, 248); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9em; }
code { background-color: rgb(243, 244, 244); padding: 0px 2px; }
.md-fences { margin-bottom: 15px; margin-top: 15px; padding-top: 8px; padding-bottom: 6px; }
.md-task-list-item > input { margin-left: -1.3em; }
@media print {
html { font-size: 13px; }
table, pre { break-inside: avoid; }
pre { overflow-wrap: break-word; }
}
.md-fences { background-color: rgb(248, 248, 248); }
#write pre.md-meta-block { padding: 1rem; font-size: 85%; line-height: 1.45; background-color: rgb(247, 247, 247); border: 0px; border-radius: 3px; color: rgb(119, 119, 119); margin-top: 0px !important; }
.mathjax-block > .code-tooltip { bottom: 0.375rem; }
.md-mathjax-midline { background: rgb(250, 250, 250); }
#write > h3.md-focus::before { left: -1.5625rem; top: 0.375rem; }
#write > h4.md-focus::before { left: -1.5625rem; top: 0.285714rem; }
#write > h5.md-focus::before { left: -1.5625rem; top: 0.285714rem; }
#write > h6.md-focus::before { left: -1.5625rem; top: 0.285714rem; }
.md-image > .md-meta { border-radius: 3px; padding: 2px 0px 0px 4px; font-size: 0.9em; color: inherit; }
.md-tag { color: rgb(167, 167, 167); opacity: 1; }
.md-toc { margin-top: 20px; padding-bottom: 20px; }
.sidebar-tabs { border-bottom: none; }
#typora-quick-open { border: 1px solid rgb(221, 221, 221); background-color: rgb(248, 248, 248); }
#typora-quick-open-item { background-color: rgb(250, 250, 250); border-color: rgb(254, 254, 254) rgb(229, 229, 229) rgb(229, 229, 229) rgb(238, 238, 238); border-style: solid; border-width: 1px; }
.on-focus-mode blockquote { border-left-color: rgba(85, 85, 85, 0.12); }
header, .context-menu, .megamenu-content, footer { font-family: "Segoe UI", Arial, sans-serif; }
.file-node-content:hover .file-node-icon, .file-node-content:hover .file-node-open-state { visibility: visible; }
.mac-seamless-mode #typora-sidebar { background-color: var(--side-bar-bg-color); }
.md-lang { color: rgb(180, 101, 77); }
.html-for-mac .context-menu { --item-hover-bg-color: #E6F0FE; }
#md-notification .btn { border: 0px; }
.dropdown-menu .divider { border-color: rgb(229, 229, 229); }
.ty-preferences .window-content { background-color: rgb(250, 250, 250); }
.ty-preferences .nav-group-item.active { color: white; background: rgb(153, 153, 153); }
-->

Python学习day42-数据库的基本操作(1)

表的详细操作及引擎

上文中我们已经了解到创建表的语法,但其实那种写法并不完整,完整的写法应该是下面这种:

 
 
 
xxxxxxxxxx
38
 
 
 
 
1
create table 表名(
2
(属性名1 类型(长度) 约束条件)
3
    ...
4
    (属性名n 类型(长度) 约束条件)
5
)engine=引擎(InnDB) DEFAULT charset=utf-8
6
7
# 修改表名
8
alter table 旧表名 rename 新表名;
9
10
# 修改字段名:
11
alter table 表名 change 旧字段 新字段 新字段数据类型(长度);
12
13
# 修改字段属性:
14
alter table 表名 modify 字段 新类型(长度);
15
16
# 查询,注意,数据库查询时候是不区分大小写的,包括修改字段类型时候也不区分大小写
17
'''
18
我们可以看到,表是可以设定自己的字符编码的,另外一方面,每个表是有自己的引擎(engine)的,所以引擎是基于表的概念,这点非常重要,引擎不是以数据库为基础的,而是每个表都会有自己的一个引擎,表的引擎其实又叫表类型.
19
20
那么引擎究竟是什么东西呢?
21
在我们的现实生活中,提到引擎也许会想到汽车,引擎是一个汽车最核心的东西,那么在mysql里面,引擎其实就是一种存储技术,不同的引擎所用的存储机制,索引的技巧以及锁定数据的方法都不尽相同,所以用不同的引擎对于我们日后数据的存取都会造成比较大的影响.
22
23
mysql5.6支持的引擎非常多,包括InnoDB、MyISAM、MEMORY、CSV、BLACKHOLE、FEDERATED、MRG_MYISAM、ARCHIVE、PERFORMANCE_SCHEMA
24
下面我们只需要了解几个常用的引擎就可以了
25
1. InnoDB,是最常用的引擎,主要用于事务处理应用程序,支持外键以及行级锁,相比MyISAM速度较慢,但是非常准确,安全性高
26
2. MyISAM,如果应用主要以读和插入为主,很少有更新或者删除操作,用MySIAM是最好的引擎方式,其插入数据的速度比InnoDb快了好几个量级
27
3. BLACKHOLE,黑洞引擎,顾名思义,就是黑洞,所有的数据放进去都不会存下来,都会直接消失,适用场景较少,通常是放进去一些垃圾数据,或者不重要的系统缓存
28
4. Memory,是把数据存放于内存中的一种方法,适用于对数据进行快速检索的场景,但其实不常用,因为我们都知道内存中存储容易丢失,安全性较差,而且如果真的需要用内存来存取数据的话有更好的方法,比如redis等.
29
30
通常如果我们面试中被问到数据库优化的问题,一般都是涉及到引擎的问题,可以尝试从MyISAM作为突破口来回答.
31
'''
32
33
# 引擎的定义形式只需要在创建表的时候在后面加上即可
34
35
create table t1(id int)engine=innodb;
36
create table t2(id int)engine=myisam;
37
create table t3(id int)engine=blackhole;
38
create table t4(id int)engine=memory;
 
 

mysql支持的数据类型

类型 大小 范围(有符号) 范围(无符号)unsigned约束 用途
TINYINT 1 字节 (-128,127) (0,255) 小整数值
SMALLINT 2 字节 (-32 768,32 767) (0,65 535) 大整数值
MEDIUMINT 3 字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值
INT或INTEGER 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
BIGINT 8 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值
FLOAT 4 字节 float(255,30) (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度 浮点数值
DOUBLE 8 字节 double(255,30) (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度 浮点数值
DECIMAL 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 double(65,30) 依赖于M和D的值 依赖于M和D的值 小数值

上表借鉴于nickchen的博客https://www.cnblogs.com/nickchen121/p/11146370.html此人非常有才,平生罕见,么么啪~

整型

 
 
 
x
 
 
 
 
1
'''类型
2
tinyint:1字节 -128~127 默认长度为4
3
smallint:2字节 -32768~32767 默认长度为6
4
mediumint:3字节 -8388608~8388 607
5
int:4字节 -2147483648~2147483647 默认长度为11
6
bigint:8字节 -9233372036854775808~9223372036854775807
7
'''
8
'''约束 *
9
unsigned:无符号
10
zerofill:0填充
11
'''
12
# 不同类型所占字节数不一样, 决定所占空间及存放数据的大小限制
13
14
# eg:
15
create table tb1(x tinyint,y smmallint,z int);
16
# 默认长度4611
17
# 默认长度是可以手动定义的,以手动定义的为准,但支持比默认长度短的,不支持比默认长度长的,一般不会手动定义int长度,都是用默认的长度
18
19
# 插入数据
20
insert into tb1 values(128,32768,32768);
21
# 结果:127,32767,32768
22
# 结论:整型的长度由所占字节(取值范围)决定,可以自定义长度,但是不影响所占字节
23
# 所有整型变量的长度一般都省略不写
24
25
# 整型约束
26
27
# unsigned无符号数
28
create table tb2(x tinyint unsigned);# 取值范围:0~255,因为定义了unsigned就是无符号数,原来的负数不能取,会从0开始取
29
insert into tb2 valus(256),(-1)# 结果:255,0
30
31
# 0填充约束,位数不足用0在数字的左边填充,注意一点的是,这里仅在查询中显示0,并不是实际在数据库中添加的0
32
create table tb2(x tinyint unsigned zerofill);
33
insert into tb2 valus(10);# 结果:010
 
 

浮点型

 
 
 
xxxxxxxxxx
1
19
 
 
 
 
1
'''类型
2
float:4字节,3.4E–38~3.4E+38 *
3
double:8字节,1.7E–308~1.7E+308
4
decimal:M要比D的最大值基础上+2
5
'''
6
'''
7
宽度:
8
限制存储宽度
9
(M, D) => M为位数,D为小数位,即总位数至少要比小数位多两位
10
float(255, 30):精度最低,最常用,一般精度到小数点后两位,用来表示金额等
11
double(255, 30):精度高,占位多
12
decimal(65, 30):字符串存,全精度,比较方便
13
'''
14
15
# 建表:
16
create table t4 (age float(256, 30)); # Display width out of range for column 'age' (max = 255)
17
create table t5 (age float(255, 31)); # Too big scale 31 specified for column 'age'. Maximum is 30.
18
19
# 上面两种报错是最常见的两种报错,第一个是位数超过限制,第二个是小数位超过限制,这点我们在创建表的时候要注意,插入数据的时候也要注意
20
21
create table t6 (age float(255, 30));
22
create table t6 (age double(255, 30));
23
create table t6 (age decimal(32, 30));# 在合理取值范围,且精度没问题
 
 

字符串

 
 
 
xxxxxxxxxx
1
10
 
 
 
 
1
'''类型
2
char:定长
3
varchar:不定长
4
'''
5
'''宽度
6
限制存储宽度
7
char(4):以4个字符存储定长存储数据,效率高,
8
varchar(4):数据长度决定字符长度,为可变长度存储数据
9
'''
10
11
# 当然数据库支持的字符串类型肯定不止这两种,只是这两种最常用,其余还有包括tinyblob,tinytext,blob,text,mediumblob,mediumtext,longblob,longtext等等数据类型.我们只需要熟悉这两种即可.
12
13
# 从数据库存取速度优化的角度来说,char的效率要高于varchar,但是在某些场景下varchar更节省资源空间,所以实际情况下是用char还是用varchar要根据情况而定.
14
15
# 建表:
16
create table ts1(s1 char(4),s2 varchar(4));
17
18
insert into ts1 values('abcde','xyzabc');
19
'''
20
重点: 存储数据的方式=> 数据库优化
21
char: 一定按规定的宽度存放数据, 以规定宽度读取数据, 通常更占空间
22
varchar: 首先根据数据长度计算所需宽度, 并在数据开始以数据头方式将宽度信息保存起来, 是一个计算耗时过程, 取先读取宽度信息,以宽度信息为依准读取数据, 通常节省空间,但是存取效率和char有差距
23
'''
 
 

时间类型

 
 
 
x
 
 
 
 
1
'''类型
2
year:yyyy(1901/2155)
3
date:yyyy-MM-dd(1000-01-01/9999-12-31)
4
time:HH:mm:ss
5
datetime:yyyy-MM-dd HH:mm:ss(1000-01-01 00:00:00/9999-12-31 23:59:59)
6
timestamp:yyyy-MM-dd HH:mm:ss(1970-01-01 00:00:00/2038
7
结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07)
8
'''
9
10
# 建表:
11
create table td1(my_year year,my_date date,my_time time);
12
create table td2(t1 timestamp);
13
14
insert into td1 values(1666,'8888-8-8','8:8:8')# 时间需要在取值范围内
15
insert into td1 values(3,'8888-8-8','8:8:8') # 2003
16
17
# timestamp时间的下限是19700101080001,低于这个值会报错
18
insert into t2 values (19700101080000);
19
# ERROR 1292 (22007): Incorrect datetime value: '19700101080000' for column 't1' at row 1
20
21
# timestamp时间的上限是2038-01-19 11:14:07,高于这个值会报错
22
insert into t2 values ('2038-01-19 11:14:08');
23
# ERROR 1292 (22007): Incorrect datetime value: '2038-01-19 11:14:08' for column 't1' at row 1
24
25
# datetime VS timestamp
26
datetime:时间范围,不依赖当前时区,8字节,可以为null
27
timestamp:时间范围,依赖当前时区,4字节,有默认值CURRENT_TIMESTAMP
 
 

枚举与集合

 
 
 
x
 
 
 
 
1
'''
2
ENUM中文意思就是枚举,他的值是通过创建表的时候事先定义好的几个选项中选择所得到的,需要注意的是ENUM只能单选不能多选,多选要用SET
3
其实就是为某一个字段提供选项:
4
枚举:只能单选,非安全模式不选的话会报警,安全模式不选会报错
5
    集合:可以多选,可以不选,都不会报错
6
'''
7
8
# 建表
9
# enum,set默认值为NULL
10
create table tc1 (
11
name varchar(20),
12
gender enum('male','female','wasai'),
13
hobbies set('male','female','wasai')
14
);
15
16
insert into tc1 values('rocky','wasaiwasai','weizhi');
17
18
# enum,set手动设置默认值为'male'和'wasai'
19
create table tc2 (
20
name varchar(20),
21
gender enum('male','female','wasai')default 'male',
22
hobbies set('male','female','wasai')default 'wasai'
23
);
24
insert into tc2 values('rocky','wasaiwasai','weizhi');
25
# 非安全模式下,赋值错误会警告,然后默认用空字符填充,安全模式下则会直接报错
26
insert into tc2(name) values('rocky')
27
# 如果对sex,hobbies两个字段外的其他字段进行赋值,这两个字段才会取到默认值
28
29
# 注意,对set类型的字符串赋值的时候,需要用一个引号包裹住(注意,只能有一个引号,多余引号就会插入失败),并且在引号内部用逗号将选项隔开,且不能添加空格或者其他任何额外的字符
30
insert into tc2 values('rocky1','female','male,female')
 
 

mysql表的约束条件

通过之前的学习我们都知道数据库是个存储数据的地方,那么但凡是数据,都应该会有自己的格式,所以一个数据库内的数据,或者说同一个表内的数据,其格式应该都是相同的,所以,为了防止不合规范的数据进入我们的数据库,我们就要用一些约束条件来规范用户对数据的插入,修改删除等操作.

比较常用的约束条件有以下几种:

  1. primary key:主键,每一条数据的唯一标识,如果我们没有对于数据的唯一性或者自增性进行设置,那么主键就会是系统默认分配的一个数据,是隐藏字段.主键所起到的主要作用就是优化查询速度.
  2. foreign key:外键,用于指定改行记录从属于主表中的一条记录,主要用于参照完整性.
  3. unique:唯一性,加上该约束条件的数据唯一,即同样的数据不能重复插入该表,unique还可以规定联合唯一,比如我们创建一个表用来存储登录用户的IP地址和端口号,这是两个字段,我们就需要把IP和端口绑定起来作为一个联合唯一的约束条件,语句为unique(ip,port)
  4. auto_increment:自增,顾名思义,每次插入数值,若没有给该字段赋值,该字段就会自动加+1,所以自增的字段通常为整型或浮点型.这里要注意的一点是,我们在删除有自增的数据的表的时候,要用truncate,,不能用delete,delete只能删除数据本身,不能删除其索引号以及各种内层的数据关系,但是truncate可以.
  5. not null:不为空,即加了该条件的值不能为空值,如如若为空值则会报错,通常用在一些不允许填空值的场合,比如注册时用户的用户名,以及出生人的性别.
  6. default:默认值,即若该行字段为多个字段,如果我们对默认值以外的字段赋值,并没有给有默认值的字段赋值,那么有默认值的字段就会自动被赋予默认值
 
 
 
x
10
 
 
1
create table td1(x int,y int default 0,z int default 100);
2
3
insert into td1(x) values(666);
4
5
6
create table td2(x int not null,y int default 0,z int default 100);
7
8
insert into td2(x) values(666);# (666,0,100)
9
insert into td2(y) values(666);# (0,666,100)如果是安全模式这里会报错,因为x定义不能为null,而我们没有给x赋值,非安全模式是给其默认了一个0,安全模式会直接报错
 
 
10
insert into td2(z) values(666);# (0,0,666)如果是安全模式这里会报错,理由同上
 
 

Python学习day42-数据库的基本操作(1)的更多相关文章

  1. Python连接oracle数据库的基本操作

    1,创建数据库连接connect和关闭数据库连接close 1.1 创建数据库连接的三种方式: 方法一:用户名.密码和监听分开写 import cx_Oracle db=cx_Oracle.conne ...

  2. Python学习笔记——MySQL的基本操作(2)

    1 运算符操作(配合查.修.删操作) 数据库的语法结构 查:select  *  from 表名 where 字段名 运算符 数字/字符; 改:update 表名 set 字段名=值,... wher ...

  3. 从零开始的Python学习Episode 7——文件基本操作

    文件基本操作 一.打开文件 f = open('11','r')#open('file path','mode') 创建一个文件对象 文件有多种打开模式: 1. 'r':新建一个文件对象以只读方式打开 ...

  4. Python学习之数据库初识

    9 数据库 9.1 数据库的初识 ​ 数据库是可以独立运行的,并且可以对数据的增删改查提供高效便捷方式的工具. 数据库解决的问题: ​ 解决了操作文件的效率和便捷问题 ​ 解决了多个服务同时使用数据时 ...

  5. Python学习之数据库

    9.6 表的查询 [结构]select distinct 字段1,字段2 from 表名 where 条件 group by 字段 having 筛选 order by 字段 limit 限制条数 [ ...

  6. python操作oracle数据库

    本文主要介绍python对oracle数据库的操作学习 包含:oracle数据库在Windows操作系统下的安装和配置.python需要安装的第三方拓展包以及基本操作的样例学习. 1          ...

  7. python对oracle数据库的操作

    1          Oracle数据库 1.1       Oracle环境配置&客户端连接 1.1.1          下载安装Oracle绿色版客户端instantclient: 到o ...

  8. Python学习系列(七)( 数据库编程)

    Python学习系列(七)( 数据库编程)        Python学习系列(六)(模块) 一,MySQL-Python插件       Python里操作MySQL数据库,需要Python下安装访 ...

  9. [Python] 学习笔记之MySQL数据库操作

    1 Python标准数据库接口DB-API介绍 Python标准数据库接口为 Python DB-API,它为开发人员提供了数据库应用编程接口.Python DB-API支持很多种的数据库,你可以选择 ...

随机推荐

  1. 常见PID里面的像素大小

    因为tensorflow/models里faster R-cnn目前识别的好像是按照像素比上图片大小来识别的,所以在这里统计一下各个元件的像素大小的范围 DCS:70~200

  2. YARN设计思路

  3. 廖雪峰Java16函数式编程-2Stream-7其他操作

    1. 排序 Stream<T> sorted(); //按元素默认大小排序(必须实现Comparable接口) Stream<T> sorted(Comparator<? ...

  4. thinkphp 输入变量

    在Web开发过程中,我们经常需要获取系统变量或者用户提交的数据,这些变量数据错综复杂,而且一不小心就容易引起安全隐患,但是如果利用好ThinkPHP提供的变量获取功能,就可以轻松的获取和驾驭变量了. ...

  5. type元类创建类的方法

    一.代码 class_name='car' dict_name={} bases=(object,) class_body=''' def __init__(self,name): self.name ...

  6. JavaScript中的浏览器对象模型

    浏览器对象模型 1.浏览器引入JavaScript 1.直接在HTML文件中引入 首先第1种方式就是直接在HTML文档里面引入JavaScript代码.在维护一些老项目的时候,经常 可以看到J ava ...

  7. Linux和Windows下ping命令详解

    转:http://linux.chinaitlab.com/command/829332.html 一.Linux下的ping参数 用途 发送一个回送信号请求给网络主机. 语法 ping [ -d] ...

  8. Java-Maven-pom.xml-porject-parent:parent

    ylbtech-Java-Maven-pom.xml-porject-parent:parent 1.返回顶部 1.Inherit defaults from Spring Boot <!-- ...

  9. day23_3_configparse

    #!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...

  10. re.groups取出来的空元祖??

    源自学习笔记: day23_1_re_ groups方法取出来的字符是空的元组??为啥? ''' # ------------------------------------------------- ...