MySQL数据库操作语句(补充1)(cmd环境运行)

一.字符串类型

enum枚举类型

 1 /*
 2 也叫做枚举类型,类似于单选!
 3 如果某个字段的值只能从某几个确定的值中进行选择,一般就使用enum类型,
在定义的时候需要将该字段所有可能的选项都罗列出来:
4 5 */ 6 7 create table test_enum( 8 gender enum('male','female','secret'); 9 ); 10 11 -- 而在插入数据的时候,只能在规定的选项中的进行选择: 12 insert into test_enum values('male'); 13 insert into test_enum values('female');

 -- 数据库中实际存储的其实是整型数据!

1 -- 插入female
2 insert into test_gender values(2); 

text 

在实际的开发中,自由输入区一般都用text类型,比如新闻正文,博客正文等!

1 create table news(
2     news_id int(11) primary key  auto_increment  ,
3     content text 
4 );

set

/*
也叫做集合类型,类似于多选项!
如果一个字段的值只能是某个或某几个选项的值,最好使用set类型
同enum类型一样,在定义的时候也需要把所有可能的选项都罗列出来:

*/
create table zifu(
  hobby set('sleep','eat','study','php','LOL','WOW')
);

insert into zifu values('sleep,php,LOL');

其实,多选项实际存储的也是整型数据:

1 -- 选择sleep 和php    1+8= 9
2 insert into zifu  values(9); 

二.列属性

null和not null

默认情况下,字段都是可以为空的,也就是该属性的缺省值为null

1 -- not null 不能为空  unsigned非特殊字符
2 create table stu(
3    name varchar(20) not null,
4    age tinyint unsigned
5 );

default

自定义默认值属性,也叫做default约束,通常就是配合not null属性一起使用,也就是说某个字段不允许为空,但是如果用户没有给该字段插入数据,就用默认值去填充!

create table user_my(
   id int primay key auto_increment
   name varchar(32) unique key default '我是没有设置唯一键内容的name'
);
-- 当然,也可以直接插入default关键字,意思就是插入默认值:

insert into user_my values(23,default);

primary key

 1.设置主键之后就不能添加重复的主键的值了

 2.主键会自动增加非空约束

定义主键方法有2:

 1 -- 1.直接在字段的后面进行设置
 2 create table stu(
 3     id int unique primary key  auto_increment   
 4 );
 5 
 6 -- 2.定义完字段后再定义主键
 7 create table stu(
 8     id int unique  auto_increment,
 9     primary key(id)
10 );

注意:

如果某个主键是一个组合主键,就只能使用第二种方式!

1 create table tea(
2     tea_name varchar(20),
3     class_id tinyint unsigned, 
4     day_num tinyint unsigned,
5     -- 定义组合主键
6     primary key(tea_name,class_id)
7 );

unique key

 1 -- 1.直接在字段后面加unique
 2 create table stu(
 3     id int unsigned primary key auto_increament,
 4     stu_id int unsigned unique key,
 5     tel char(11)  unique key
 6 );
 7 
 8 -- 2.先定义字段,后设置unique key
 9 create table stu(
10     id int unsigned primary key auto_increament,
11     stu_id int unsigned ,
12     tel char(11) ,
13     -- 定义两个唯一键
14     unique key(stu_id,tel)
15 );

auto_increment

 1 /*
 2 自动增长属性,或者叫做自动增长约束!
 3 作用是每次插入记录的时候,为某个字段的值自动加1
 4 
 5 注意:使用这个属性是有条件的!!!
 6 第一,    该字段必须为整型
 7 第二,    该字段必须存在索引(主键或唯一键)
 8 
 9 典型的,我们就是在ID主键上增加auto_increment属性!
10 当我们把主键设置为自动增长之后,主键可以直接用null来进行填充!这里不是真的把主键设置为NULL,而是启动了自动增长机制!
11 */
12 create table stu(
13   -- 设置id字段为主键,并自动增长
14   id int unsigned primary key auto_increment,
15 );
16 
17 insert into stu values(null);
18 
19 -- 设置从100开始自动增长
20 create table stu(
21    id int unsigned primary key auto_increment,
22    name varchar(20),        
23 )auto_increment 100; -- 设置自动增长从100开始

重置自动增长机制

 1 /*
 2 
 3 剖析一下truncate的语法意义:
 4 1,    把原表先drop掉!
 5 2,    按原表的表结构重新创建一个新表,名字和原表一样的!
 6 所以,truncate语句是DDL,而delete from是属于DML!
 7 所以:使用delete from语句删除所有的记录,不能重置自动增长机制!
 8 */
 9 truncate table 表名;
10 truncate 表名;

comment

1 /*
2 是指在创建表的时候,对字段的备注,注意与普通注释符的区别:
3 普通注释符不属于语句的一部分,而comment属于!!
4 */
5 create table stu(
6     id int unsigned primary key auto_increment comment '设置id字段为主键,并自动增长'
7     name varchar(20) comment '学生的姓名' 
8 );
原文地址:https://www.cnblogs.com/mrszhou/p/7460691.html