字符创函数 not null AUTO_INCREMENT字段约束

**

字符串函数:**

substr(string ,start,len) 截取:从start开始,截取len长.start 从1开始算起。
正常查询
mysql> select bTypeName from category where bTypeId=10;在这里插入图片描述
Substr截取字符串
mysql> select substr(bTypeName,1,7) from category where bTypeId=10;
在这里插入图片描述
mysql> select substr(bTypeName,2,7) from category where bTypeId=10;
#从第二个字符串截取
在这里插入图片描述
concat(str1,str2,str3…) 拼接。 把多个字段拼成一个字段输出
mysql> select concat(bName,"-----",publishing) from books;
在这里插入图片描述
大小写转换
upper()大写 : 转为大写输出
mysql> select upper(bname) from books where bId=9;
在这里插入图片描述
lower()小写:转为小写输出
mysql> select lower(bName) from books where bId=10;
在这里插入图片描述
MySQL字段约束-索引-外键

字段修饰符 (约束) NULL和NOT NULL修饰符

null和not null
创建带修饰符的表,测试null和not null字段区别
mysql> use HA; #使用HA数据库
mysql> create table worker(id int not null,name varchar(8) not null,pass varchar(20) not null); #创建worker表,使用null和not null字段约束
mysql> insert into worker values(1,‘HA’,‘123456’); #插入数据
mysql> insert into worker values(1,‘LB’,null); #在not null字段插入null值
ERROR 1048 (23000): Column ‘pass’ cannot be null #不能插入null值
mysql> insert into worker values(2,‘HPC’,’’);
注:NOT NULL 的字段是不能插入“NULL”值,可以插入“空值”
null和not null区别
1、字段类型是not null,为什么可以插入空值?
1、空值是不占用空间的
2、mysql中的NULL其实是占用空间的,下面是来自于MYSQL官方的解释
“NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.”
#“空列需要行中的额外空间来记录其值是否为空。
比如:一个杯子,空值’'代表杯子是真空的,NULL代表杯子中装满了空气,虽然杯子看起来都是空的,但是里面是有空气的。
2、为什么not null的效率比null高?

NULL 其实并不是空值,而是要占用空间,所以mysql在进行比较的时候,NULL 会参与字段比较,所以对效率有一部分影响。
3、判断字段不为空的时候,到底要 select * from table where column <> ‘’ 还是要用 select * from table where column is not null 呢?
创建表,插入数据
mysql> create table test(col1 varchar(10) not null, col2 varchar(10) null)ENGINE=MyISAM;
mysql> insert into test values(’’,null);
mysql> insert into test values(‘1’,‘2’);
mysql> insert into test values(’’,‘1’);
在这里插入图片描述
测试
mysql> select * from test where col1 is not null;
在这里插入图片描述
mysql> select * from test where col1 <>’’;
在这里插入图片描述
空值表示不占空间,null占用空间
DEFAULT 设定字段的默认值
为字段指定默认的值
mysql> create table test2(name varchar(8) not null,dept varchar(25) default ‘SOS’);
mysql> insert into test2 (name) values (‘kko’); #插入值
mysql> select * from test2; #没有给dept字段插入值,发现它默认给了SOS值
在这里插入图片描述
注意:
1、如果字段没有设定default ,mysql依据这个字段是null还是not null,如果为可以为null,则为null。如果不可以为null,报错。
*

AUTO_INCREMENT字段约束*

自动增长
只能修饰 int字段。 表明mysql应该自动为该字段生成一个唯一没有用过的数(每次在最大ID值的基础上加1。特例:如果目前最大ID是34,然后删除34,新添加的会是35.)。对于主键,这是非常有用的。 可以为每条记录创建一个惟一的标识符。
mysql> create table items ( id int not null auto_increment primary key , label varchar(20) not null);
mysql> insert into items (label) values (‘aaba’);
mysql> insert into items values (9,‘aaba’);
再插入一条id将为多少
mysql> insert into items (label) values (‘abc’);
mysql> select * from items;
在这里插入图片描述
再次插入一条数据,删除,再次添加,查看它的id
mysql> insert into items (label) values (‘abcs’);
mysql> select * from items;
在这里插入图片描述
mysql> delete from items where label=‘abcs’;
mysql> insert into items (label) values (‘abcsw’);
mysql> select * from items;
在这里插入图片描述
查看id值为12,不是11,说明主键约束唯一
清除表中的记录
方法一:DELETE
DELETE 不加WHERE条件,清空所有表记录。但是DELETE不会清零AUTO_INCREMENT 值
mysql> delete from items;
mysql> select * from items;
Empty set (0.00 sec) #表里没有内容了
mysql> insert into items (label) values (“aaaa”);
mysql> select * from items;
在这里插入图片描述
发现不能清空AUTO_INCREMENT 值
方法二:truncate
truncate
作用: 删除表的所有记录,并清零auto_increment 值。新插入的记录从1开始。
语法: truncate table name;
mysql> truncate table items;
mysql> select * from items; #清空表中的数据
Empty set (0.00 sec)
mysql> insert into items values(null,‘abv’);
mysql> select * from items;

在这里插入图片描述
mysql> insert into items(label)values(‘hkuyb’);
mysql> select * from items;
在这里插入图片描述

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值