From f0aa5e064ce2644e9cc44be6bca94a7565c970e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=95=8F?= <3234934487@qq.com> Date: Mon, 20 Feb 2023 22:00:56 +0800 Subject: [PATCH] =?UTF-8?q?mysql=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20210220 \344\275\234\344\270\232.md" | 39 +++ .../20230220 \347\254\224\350\256\260.md" | 232 ++++++++++++++++++ 2 files changed, 271 insertions(+) create mode 100644 "56 \350\265\265\346\225\217/20210220 \344\275\234\344\270\232.md" create mode 100644 "56 \350\265\265\346\225\217/20230220 \347\254\224\350\256\260.md" diff --git "a/56 \350\265\265\346\225\217/20210220 \344\275\234\344\270\232.md" "b/56 \350\265\265\346\225\217/20210220 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..41255fa --- /dev/null +++ "b/56 \350\265\265\346\225\217/20210220 \344\275\234\344\270\232.md" @@ -0,0 +1,39 @@ +-- 1、创建数据库test01_market + +create database test01_maeket charset utf8; +-- +-- 2、创建表格customers +use test01_maeket; +create table customers( +c_num int(11), +c_name varchar(50), +c_contact varchar(50), +c_city varchar(50), +c_birth date +); +-- +| -- | 字段名 | 数据类型 | +| ------------------------------------------------------------ | --------- | ----------- | +| -- | c_num | int(11) | +| -- | c_name | varchar(50) | +| -- | c_contact | varchar(50) | +| -- | c_city | varchar(50) | +| -- | c_birth | date | +| -- | | | +| -- **要求3:**将c_contact字段移动到c_birth字段后面 | | | +| alter table customers modify c_contact varchar(50) after c_birth; | | | +| -- | | | +| -- **要求4:**将c_name字段数据类型改为 varchar(70) | | | +| alter table customers modify c_name varchar(70); | | | +| -- | | | +| -- **要求5:**将c_contact字段改名为c_phone | | | +| alter table customers change c_contact c_phone varchar(50); | | | +| -- | | | +| -- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) | | | +| alter table customers add c_gender char(1) after c_name; | | | +| -- | | | +| -- **要求7:**将表名改为customers_info | | | +| alter table customers rename to customers_info; | | | +| -- | | | +| -- **要求8:**删除字段c_city | | | +| alter table customers_info drop column c_city; | | | \ No newline at end of file diff --git "a/56 \350\265\265\346\225\217/20230220 \347\254\224\350\256\260.md" "b/56 \350\265\265\346\225\217/20230220 \347\254\224\350\256\260.md" new file mode 100644 index 0000000..8914a44 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20230220 \347\254\224\350\256\260.md" @@ -0,0 +1,232 @@ +1.不分大小写 + +2.一般以;结尾 + +3.库名,表名,字段名(列名),支持英文字母,_,数字 + +4.名称中不要用空格 + +5.名称中,不用sql语句的关键字 + +6.‘ “ `(反引号|飘号),用来标记名称 + +7.--注释,#注释内容,/* */多行注释,(ctel+/) + +8.数值直接写,不用符号包裹,字符串和时间数型用单引号包裹 + +### 数据类型 + +1.约束类型 + +2.规范数据空间的占用(内存) + +## 类型 + +#### 数值型 小数+整数 + +1.整形 int(M)这里的M不是限制长度,单独使用毫无意义,配合zerofill使用(补0) + +2.浮点 float(M,N) double(M,N) M表示限制整数+小数只能存放M长度,N限制小数的长度。如果小数的部分超过了N,会自动截断,并四舍五入。 + +3.定点 decimal(M,N) 默认(10,0),其他同上 + +#### 字符型 + +1.固定长度的 char 默认是1,最大是255 + +2.可变长度的 varchar(M) 这里的M不可缺省 + +#### 时间类型 + +1.data 年月日 + +2.time 时分秒 + +3.year 年 + +4.datatime + +5.timestamp 时间戳 + +#### enum枚举类型 + +类似单选框中的选项,只能选一个。 enum(1,2,3,4,5) + +#### set集合类型 + +类似多选框,可以选0个或是多个,但也只能选里面有的。set(1,2,3,4,5) + +### #一些数据库的属性 + +1.unsigned 无符号的,表示正数不能负数 + +2.null 允许为空。not null 不允许为空。 + +3.default 默认值 + +4.auto_increment 自增 + +## # DDL + +创建数据库 + +```MYSQL +create database 数据库名 charset 字符集; +``` + +查看所有数据库 + +```mysql +show databases; +``` + +查看数据库的定义语句 + +```mysql +show create database +``` + +修改数据库编码 + +```mysql +#修改数据库字符集和校对规则 +ALTER DATABASE 数据库名称 CHARACTER SET 字符集名称 COLLATE 字符集对应校对规则; +``` + +删除数据库 + +```mysql +drop database 数据库名; +``` + +使用数据库 + +```mysql +use 数据库名; +``` + +查看数据库中的所有表格 + +```mysql +use 数据库名; +show tables; +或 +show tables from 数据库名; +``` + +创建表格 + +```mysql +create table 数据表名称( + 字段名 数据类型, + 字段名 数据类型 + ... +); +``` + +查看表的定义信息 + +```mysql +show create table 表名称; +``` + +查看表结构 + +```mysql +desc 表名称; +``` + +修改表名称 + +```mysql +alter table 旧表名 rename 【to】 新表名; +rename table 旧表名称 to 新表名称; +``` + +删除表格 + +```mysql +drop table 表名称; +``` + +删除字段 + +```mysql +alter table 表名称 drop 【column】 字段名称; +``` + +增加字段 + +```mysql +alter table 表名称 add 【column】 字段名称 数据类型; +alter table 表名称 add 【column】 字段名称 数据类型 first; +alter table 表名称 add 【column】 字段名称 数据类型 after 另一个字段; +``` + +修改字段的数据类型 + +```mysql +alter table 表名称 modify 【column】 字段名称 新的数据类型; +``` + +修改字段名称 + +```mysql +alter table 表名称 change 【column】 旧字段名称 新的字段名称 新的数据类型; +``` + +修改字段位置 + +```mysql +alter table 表名称 modify 【column】 字段名称 数据类型 first; +alter table 表名称 modify 【column】 字段名称 数据类型 after 另一个字段; +``` + +# DML + +添加一条记录到表中 + +```mysql +insert into 表名称 values(值列表);#值列表中的值的顺序、类型、个数必须与表结构一一对应 + +insert into 表名称 (字段列表) values(值列表); +``` + +添加多条记录到表中 + +```mysql +insert into 表名称 values(值列表),(值列表),(值列表); #值列表中的值的顺序、类型、个数必须与表结构一一对应 + +insert into 表名称 (字段列表) values(值列表),(值列表),(值列表); +``` + +修改所有行 + +```mysql +update 表名称 set 字段名 = 值, 字段名 = 值; #给所有行修改 +``` + +修改部分行 + +```mysql +update 表名称 set 字段名 = 值, 字段名 = 值 where 条件; #给满足条件的行修改 +``` + +删除部分行的数据 + +```mysql +delete from 表名称 where 条件; +``` + +删除整张表的数据,表结构留下 + +```mysql +delete from 表名称; +``` + +截断表,清空表中的数据,只留有表结构 + +```mysql +truncate 表名称; +``` + -- Gitee