当前位置:网站首页>MySQL关于表的知识点,进来学习!
MySQL关于表的知识点,进来学习!
2022-08-09 09:29:00 【农场主er】
一、表的创建
语法格式如下:create table 表名(字段名 数据类型(长度约束),...);。
1、常用数据类型
| 类型 | 解释 |
|---|---|
int(长度) | 整型 |
bigine(长度) | 长整型 |
char(长度) | 定长字符串,存储空间固定 |
varchar(长度) | 变长字符串,存储空间等于实际数据空间 |
double(有效数字位数,小数位) | 数值型 |
foat(有效数字位数,小数位) | 数值型 |
date | 日期型,格式为:年-月-日 |
datetime | 日期型,格式为:年-月-日 时:分:秒 |
time | 日期型,格式为:时:分:秒 |
blob | 二进制大对象,适合音频、图片 |
clob | 字符大对象,适合大文本 |
2、示例
创建student表:学号、姓名、班级、所属学校、性别
create table student(
id int(10),
name varchar(255),
class varchar(255),
school char(19) default 'YuCai Middle School',
gender char(1)
);
注释
- 字段可以在创建时指定默认值,如果不指定默认值为NULL。
二、表数据的增删改
1、添加:insert
语法格式如下:
insert into 表名(字段名,...) values(对应的赋值,...);insert into 表名 select语句;:将select的查询结果作为数据插入到表中。
注释
- 如果不指定全部的字段名,除了已赋值的字段,余下的为默认值;
- 字段名和对应的值顺序上要一一对应;
- 字段名的顺序没有必要跟建表时一致;
- 如果想一次性插入多条数据,
values后可以有多个,连接的小括号; - 如果省略字段名,赋值必须要与创建表时的字段顺序对应,且即便已经有默认值的字段也要赋值。
示例insert into student(id,name,class,gender) values(1,'Alice','1-1','M');
mysql> select * from student;
//1-1表示1年级1班,M表示男性
+------+-------+-------+---------------------+--------+
| id | name | class | school | gender |
+------+-------+-------+---------------------+--------+
| 1 | Alice | 1-1 | YuCai Middle School | M |
+------+-------+-------+---------------------+--------+
给date型数据赋值的方法
- 按照规定的格式给出日期:例如
'2000-01-01'就是合适的表达方式; - 格式转换,例如
str_to_date('2000-01-01','%Y-%m-%d'); now()获取当前日期或时间。
2、修改:update
语法格式如下:update 表名 set 字段名=值,... where 条件,如果不加条件则修改表中字段对应的所有数据。
示例
插入时不小心输错了Alice的性别,修改如下:update student set gender='F' where id=1;。
mysql> select * from student;
+------+-------+-------+---------------------+--------+
| id | name | class | school | gender |
+------+-------+-------+---------------------+--------+
| 1 | Alice | 1-1 | YuCai Middle School | F |
+------+-------+-------+---------------------+--------+
3、删除:delete
语法格式如下:delete from 表名 where 条件;,如果不加条件则删除表中所有数据,这种方式可回滚,不是永久性的删除。如果想要永久删除数据,截断表,可用truncate table 表名。
如果要删除表,可用drop table 表名或drop table if exists 表名。
三、表的复制
语法格式如下:create table 表名 as select语句;表示将select的查询字段当作一张新表,同时数据也被复制过去。
示例create table student_copy as select id,name from student;
//查询表结构
mysql> desc student_copy;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
//查询表中所有数据
mysql> select * from student_copy;
+------+-------+
| id | name |
+------+-------+
| 1 | Alice |
+------+-------+
表的C(create)R(read)U(update)D(delete)就介绍到这里,欢迎评论区交流~
边栏推荐
猜你喜欢

Another implementation of lateral view explode

m个样本的梯度下降

The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented

字典

本体开发日记01-Jena配置环境变量

Arrays类、冒泡排序、选择排序、插入排序、稀疏数组!

软件测试面试思路技巧和方法分享,学到就是赚到

.equals==

mysql简单安装

How much do you know about the mobile APP testing process specifications and methods?
随机推荐
How much do you know about the mobile APP testing process specifications and methods?
手机APP测试流程规范和方法你知道多少?
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
归并排序
4. Character stream
测试计划包括哪些内容?目的和意义是什么?
银联最新测试工程师笔试题目,你能得多少分?
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
2.线程创建
功能自动化测试实施的原则以及方法有哪些?
6.File类
4.泛型和工具类
软件测试流程包括哪些内容?测试方法有哪些?
class object property method class member
2048小游戏成品源码
日期操作比较全面得代码
接口测试的基础流程和用例设计方法你知道吗?
Go-指针的那些事
RPC服务远程漏洞
3. Practice the Thread