10 表的增删改(一)

1 建表的语法格式:

create table 表名(字段名1 数据类型, 字段名2 数据类型, 字段名3 数据类型);

create table 表名(
		字段名1 数据类型, 
		字段名2 数据类型, 
		字段名3 数据类型
	);

表名:建议以t_ 或者 tbl_开始,可读性强。见名知意。
字段名:见名知意。
表名和字段名都属于标识符。

2 关于mysql中的数据类型?

很多数据类型,我们只需要掌握一些常见的数据类型即可。

	varchar(最长255)
		可变长度的字符串
		比较智能,节省空间。
		会根据实际的数据长度动态分配空间。

		优点:节省空间
		缺点:需要动态分配空间,速度慢。

	char(最长255)
		定长字符串
		不管实际的数据长度是多少。
		分配固定长度的空间去存储数据。
		使用不恰当的时候,可能会导致空间的浪费。

		优点:不需要动态分配空间,速度快。
		缺点:使用不当可能会导致空间的浪费。

		varchar和char我们应该怎么选择?
			性别字段你选什么?因为性别是固定长度的字符串,所以选择char。
			姓名字段你选什么?每一个人的名字长度不同,所以选择varchar。

	int(最长11)
		数字中的整数型。等同于java的int。

	bigint
		数字中的长整型。等同于java中的long。

	float	
		单精度浮点型数据

	double
		双精度浮点型数据

	date
		短日期类型

	datetime
		长日期类型

	clob
		字符大对象
		最多可以存储4G的字符串。
		比如:存储一篇文章,存储一个说明。
		超过255个字符的都要采用CLOB字符大对象来存储。
		Character Large OBject:CLOB


	blob
		二进制大对象
		Binary Large OBject
		专门用来存储图片、声音、视频等流媒体数据。
		往BLOB类型的字段上插入数据的时候,例如插入一个图片、视频等,
		你需要使用IO流才行。

3 创建一个学生表?

(1)学号、姓名、年龄、性别、邮箱地址

create table t_student(
		no int,
		name varchar(32),
		sex char(1),
		age int(3),
		email varchar(255)
	);

(2)删除表:

	drop table t_student; // 当这张表不存在的时候会报错!
   // 如果这张表存在的话,删除
    drop table if exists t_student;

4 插入数据insert (DML)

(1)语法格式:

insert into 表名(字段名1,字段名2,字段名3...) values(1,2,3);

注意:字段名和值要一一对应。什么是一一对应?
数量要对应。数据类型要对应。

insert into t_student(no,name,sex,age,email) values(1,'zhangsan','m',20,'zhangsan@123.com');
insert into t_student(email,name,sex,age,no) values('lisi@123.com','lisi','f',20,2);
insert into t_student(no) values(3);

在这里插入图片描述
(2)注意:insert语句但凡是执行成功了,那么必然会多一条记录。
没有给其它字段指定值的话,默认值是NULL。

insert into t_student(name) values('wangwu');

在这里插入图片描述

(3) insert语句中的“字段名”可以省略吗?可以

insert into t_student values(2); //错误的
// 注意:前面的字段名省略的话,等于都写上了!所以值也要都写上!
insert into t_student values(2, 'lisi', 'f', 20, 'lisi@123.com');

5 insert插入日期

(1) 数字格式化:format

select ename,sal from emp;

在这里插入图片描述
格式化数字:format(数字, ‘格式’)

select ename,format(sal, '$999,999') as sal from emp;

在这里插入图片描述(2)str_to_date:将字符串varchar类型转换成date类型
(3)date_format:将date类型转换成具有一定格式的varchar字符串类型。

  create table t_user(
		id int,
		name varchar(32),
		birth date // 生日也可以使用date日期类型
	);

  create table t_user(
		id int,
		name varchar(32),
		birth char(10) // 生日可以使用字符串,没问题。
	);

生日:1990-10-11 (10个字符)

注意:数据库中的有一条命名规范:所有的标识符都是全部小写,单词和单词之间使用下划线进行衔接。

(4) mysql> desc t_user;

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(32) | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

a.插入数据?

insert into t_user(id,name,birth) values(1, 'zhangsan', '01-10-1990'); // 1990年10月1日`

出问题了:原因是类型不匹配。数据库birth是date类型,这里给了一个字符串varchar。

怎么办?可以使用str_to_date函数进行类型转换。
str_to_date函数可以将字符串转换成日期类型date?
语法格式:
str_to_date(‘字符串日期’, ‘日期格式’)

mysql的日期格式:
%Y 年
%m 月
%d 日
%h 时
%i 分
%s 秒

insert into t_user(id,name,birth) values(1, 'zhangsan', str_to_date('01-10-1990','%d-%m-%Y'));

str_to_date函数可以把字符串varchar转换成日期date类型数据,
通常使用在插入insert方面,因为插入的时候需要一个日期类型的数据,
需要通过该函数将字符串转换成date。

b.好消息?
如果你提供的日期字符串是这个格式,str_to_date函数就不需要了!!!
%Y-%m-%d

insert into t_user(id,name,birth) values(2, 'lisi', '1990-10-01');

c.查询的时候可以以某个特定的日期格式展示吗?
date_format
这个函数可以将日期类型转换成特定格式的字符串。

select id,name,date_format(birth, '%m/%d/%Y') as birth from t_user;
	+------+----------+------------+
	| id   | name     | birth      |
	+------+----------+------------+
	|    1 | zhangsan | 10/01/1990 |
	|    2 | lisi     | 10/01/1990 |
	+------+----------+------------+

d. date_format函数怎么用?
date_format(日期类型数据, ‘日期格式’)
这个函数通常使用在查询日期方面。设置展示的日期格式。

mysql> select id,name,birth from t_user;
	+------+----------+------------+
	| id   | name     | birth      |
	+------+----------+------------+
	|    1 | zhangsan | 1990-10-01 |
	|    2 | lisi     | 1990-10-01 |
	+------+----------+------------+

以上的SQL语句实际上是进行了默认的日期格式化,
自动将数据库中的date类型转换成varchar类型。
并且采用的格式是mysql默认的日期格式:‘%Y-%m-%d’

select id,name,date_format(birth,'%Y/%m/%d') as birth from t_user;

6 date和datetime两个类型的区别?

date是短日期:只包括年月日信息。
datetime是长日期:包括年月日时分秒信息。

drop table if exists t_user;
create table t_user(
	id int,// id是整数
	name varchar(32),// name是字符串
	birth date,// birth是短日期
	create_time datetime// create_time是这条记录的创建时间:长日期类型
);

mysql短日期默认格式:%Y-%m-%d
mysql长日期默认格式:%Y-%m-%d %h:%i:%s

insert into t_user(id,name,birth,create_time) values(1,'zhangsan','1990-10-01','2020-03-18 15:49:50');

在mysql当中怎么获取系统当前时间?
now() 函数,并且获取的时间带有:时分秒信息!!!!是datetime类型的。

insert into t_user(id,name,birth,create_time) values(2,'lisi','1991-10-01',now());

7 修改update(DML)

(1)语法格式:

update 表名 set 字段名1=1,字段名2=2,字段名3=3... where 条件;

(2)注意:没有条件限制会导致所有数据全部更新。

update t_user set name = 'jack', birth = '2000-10-11' where id = 2;
+------+----------+------------+---------------------+
| id   | name     | birth      | create_time         |
+------+----------+------------+---------------------+
|    1 | zhangsan | 1990-10-01 | 2020-03-18 15:49:50 |
|    2 | jack     | 2000-10-11 | 2020-03-18 15:51:23 |
+------+----------+------------+---------------------+
   update t_user set name = 'jack', birth = '2000-10-11', create_time = now() where id = 2;

(3)更新所有?

    update t_user set name = 'abc';

8 删除数据 delete (DML)

(1)语法格式?

    delete from 表名 where 条件;

(2)注意:没有条件,整张表的数据会全部删除!

    delete from t_user where id = 2;

	insert into t_user(id) values(2);

	delete from t_user; // 删除所有!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值