From 7d2c5fc61de9cd61b044a1cdb6e42f7b617b93bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E4=BA=A8=E8=80=80?= <2640788668@qq.com> Date: Mon, 20 Feb 2023 11:57:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\344\270\216\344\275\234\344\270\232.md" | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 "25 \345\215\242\344\272\250\350\200\200/2.20\345\270\270\347\224\250\345\221\275\344\273\244\344\270\216\344\275\234\344\270\232.md" diff --git "a/25 \345\215\242\344\272\250\350\200\200/2.20\345\270\270\347\224\250\345\221\275\344\273\244\344\270\216\344\275\234\344\270\232.md" "b/25 \345\215\242\344\272\250\350\200\200/2.20\345\270\270\347\224\250\345\221\275\344\273\244\344\270\216\344\275\234\344\270\232.md" new file mode 100644 index 0000000..c731533 --- /dev/null +++ "b/25 \345\215\242\344\272\250\350\200\200/2.20\345\270\270\347\224\250\345\221\275\344\273\244\344\270\216\344\275\234\344\270\232.md" @@ -0,0 +1,187 @@ +**使用**cmd命令连接数据库 + +mysql -h 主机名 -P 端口号 -u 用户名 -p密码(中间不能有空格) + +如果是连本机:-h 用户名可以省略,端口号未修改:-P 端口号也可以省略 + +简写成:mysql -u 用户名 -p密码 + +**查看**所有的数据库:show databases: + +**创建**数据库:create database 数据库名; + +**删除**数据库:drop database 数据库名; + +**使用**数据库:use 数据库名; + +**查看**数据库的所有表格:show table from 数据库名; + +**创建**新的表格:create table 表名称( + + 字段名 数据类型, + + 字段名 数据类型 + + ); + +**查看**已定义的表结构:dese 表名称; + +**添加**一条表记录:insert into 表名称 valuse(值列表); + +**查看**一个表的数据:select*from 表名称; + +**删除**表:drop table 表名称; + + + +char(m)为固定长度的字符串,默认储存1个字符,最大存储255 + +例:create table temp( + + c1 char(2) + + ); + + insert into temp valves('数据库');#失败 + +varchar(m)可变长度的字符串,m不可缺省必须指定m + +unsigned声明该数列不允许负数 + +zerofilll不足位数用零来填充(推荐和int一起使用) + +double双精度(适合存储小数) + +float单精度 + +int整数 + +decimal定点数默认是(10,0)四舍五入时5就是5 + +double和float四舍五入时5是无限接近于5 + +float(m,n)m表示限制整数+小数一共只能存放m位的长度 + +如果小数部分长度超过n,自动四舍五入 + +时间类型:date年月日,time时分秒,year年份,datetime日期时间 + +enum枚举类型,类似单选框只能选择其中一个 + +set集合,类似多选框,可以不选也可以选 + + + +if not exists 如果不存在 + +例:如果没有该名称的数据库则创建名为该名称的数据库,否则跳过 + +if existe 如果存在 + +例:如果该数据库存在则删除该数据库否则跳过 + +修改数据库的编码 + +alter database 数据库名 character set 新的数据库名; + +添加一个表字段 + +alter table 表名称 add 字段名 数据类型; + +删除一个字段 + +alter table 表名称 change 旧字段名 新字段名 数据类型; + +修改一个字段的数据类型 + +alter table 表名称 modify 字段名称 新数据类型; + +修改字段的位置顺序 + +alter table 表名称 modify 字段名称 数据类型 after 另一个字段; + +重命名表名称 + +alter table 旧表名称 rename to 新表名称; + +添加指定字段的赋值 + +insert into 表名称(字段列表)values(值列表); + +删除某个列的一行 + +delete from 表名称 where 条件; + +例:delete from stu where sid=777; + +删除列的数据 + +update 表名称 set 列名称=null; + +## 作业 + +```mysql + +第一题 +-- 1、创建数据库 +test01_market create database test01_market; use test01_market; +-- 2、创建表格 +customers create TABLE customers( + c_num int(11), c_name VARCHAR(50), + c_comtact VARCHAR(50), + c_city VARCHAR(50), + c_birth DATE +); +-- **要求3:**将c_contact字段移动到c_birth字段后面 +alter TABLE customers modify c_comtact 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_comtact c_phone VARCHAR(50); +-- +-- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) +ALTER TABLE customers add c_gender CHAR(1); +alter TABLE customers modify c_gender VARCHAR(50) AFTER c_name; +-- +-- **要求7:**将表名改为customers_info +ALTER TABLE customers rename to customers_info; +-- +-- **要求8:**删除字段c_city +ALTER TABLE customers_info DROP c_ity; + + +第二题 +create DATABASE test02_library; +USE test02_library; +CREATE table books( +b_id int(11), +b_name varchar(50), +auto VARCHAR(100), +pice FLOAT, +pubdate YEAR, +note varchar(100), +num int(11) +); +DESC books; +-- 1、指定所有字段名称插入第一条记录 + NSERT INTO books(b_id,b_name,`authors`,price,pubdate,note,num) VALUES(1,'Tal of AAA','Dickes',23,1995,'novel',11); +-- 2、不指定字段名称插入第二记录 +INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); +-- 3、同时插入多条记录(剩下的所有记录) +INSERT INTO books(b_id,b_name,`authors`,price,pubdate,note,num) +VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0), +(4,'Lovey Day','George Byron',20,'2005','novel',30), +(5,'Old land','Honore Blade',30,'2010','law',0) +(6,'The Battle','Upton Sara',30,'1999','medicine',40), +(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +-- 4、将小说类型(novel)的书的价格都增加5。 +update books set price = price+5 where note='novel'; +-- 5、将名称为EmmaT的书的价格改为40。 +update books set price = 40 where b_name='EmmaT'; +-- 6、删除库存为0的记录 +delete from books where num=0; + +``` + -- Gitee