MySQL学习笔记_3_MySQL创建数据表(中)



MySQL创建数据表(中)

三、数据字段属性

1、unsigned【无符号】

可以让空间增加一倍
比如可以让-128~127增加到0~255

注意:只能用在数值型字段

2、zerofill【前导零】

e.g. createtable if not exists t2(num int(5) zerofill,price float(7,2)zerofill,name varchar(10));

注意:只能用在数值型字段,自动加上无符号属性

3、auto_increment【自增】 #auto自动;increment增量,增加

当插入值为:NULL,0,留空时,会自动+1;当插入已经存在的值时,则会报错

注意:只能用于整数,字段值不允许重复(需要结合其他属性实现,如:primarykey) #primary主要的;初级的,基本的。

e.g. createtable if not exists t3(id int auto_increment primary key,namechar(10));

insertinto t3(id,name) values(null,”xiaofang”); #可以连续插入n次,null可以换成0

insertinto t3(name) values(“xiaofang”);

插入时,会按照最大的数加1的顺序插入

e.g. deletefrom t3 where id >1 and id <9;

然后按照前面的语句插入

select* from t3 order by id;

deletefrom t3;

insertinto t3(name) values(“xiaofang”); # * 5

select* from t3;

insertinto t3(id,name) values(100,”ashun”);

insertinto t3(name) values(“jichang”) # * 5

select* from t3 order by id;

最佳实践:每个表最好都设置一个ID字段,设置为自增长属性,auto_increment

4、NULL和
NOTNULL

NULL:默认是空

建议:在创建表时,每个字段都不要插入空值,因为NULL值在转换为其他程序语言时存在很多不确定因素。

NOTNULL:非空

e.g. createtable if not exists t4(id int not null,name varchar(10) notnull,price double(7,2) not null) ;

5、default【缺省值】

e.g. createtable if not exists t5(id int not null default 0,name varchar(10) notnull default “NULL”,price double(7,2) not null default 0.00);

6、综合

createtable users(

idint unsigned not null auto_increment primary key,

namevarchar(30) not null default “”,

heightdouble(10,2) not null default 1.00,

ageint unsigned not null default 1,

sexvarchar(5) not null default ”man”);

四、创建索引

1、主键索引【primarykey】
#duplicate复制,使加倍 entry进入,侵入

作用:确定数据库表里一条特定数据记录的位置,一个表只能有一个主键,并且,主键的值不能为空。

建议:最好为每一个数据表定义一个主键!

e.g. 1)create table t7(id int not null auto_increment primary key,namevarchar(10));

2) createtable t7(

idint not null auto_increment,

namevarchar(10) not null ‘‘,

primarykey(id)); #在最后指定主键索引

2、唯一索引【unique】
#unique唯一的,独一无二的

都可以防止创建重复的值,但是,每个表可以有多个唯一索引

createtable if not exists users(id int not null auto_increment,namevarchar(30) not null default ‘‘ unique,age int,primary key(id));

3、常规索引【index/key】

是最重要的技术,可以提升数据库的性能,是数据库优化最先考虑的方面。索引可以提高查找的速度,但是会减慢插入,删除,修改的速度

和表一样是独立的数据对象,可以在创建表时使用,也可单独使用

单独使用时:createindex ind1 on users(name,age);

dropindex ind1 on users; #删除索引

创建时使用:createtable carts(

idint not null auto_increment,

uidint not null,

sidint not null,

primarykey(id),

keycuid(uid),

indexcsid(sid));

4、全文索引

fulltext类型索引,只能MyISAM表类型上使用,只有在varchar,char,text上使用。也可以在多个数据列上使用。

createtable books(

idint not null auto_increment,

booknamevarchar(30) not null unique,

pricedouble,

detailtext not null,

fulltext(detail),

indexind(bookname),

primarykey(id));

原始查询:select* from books where bookname like ‘%C++%‘;

现在查询:selectbookname,price from books where match(detail)against(‘C++‘);

select match(detail) against(‘C++‘) from books; #match
匹配;against倚,靠;

可以明显的查询速度!

时间: 2024-08-08 07:56:22

MySQL学习笔记_3_MySQL创建数据表(中)的相关文章

MySQL学习笔记_2_MySQL创建数据表(上)

MySQL创建数据表(上) 一.创建数据表的SQL语句模型[弱类型] CREATETABLE [IF NOT EXISTS] 表名称( 字段名1列的类型[属性][索引], 字段名2 列的类型[属性][索引], - 字段名n列的类型[属性][索引], )[表属性][表字符集]: [表名称,字段名需要自己来命名] [SQL语句并不区分大小写,但是一个文件名:在Windows下不区分大小写,但是在 Linux/UNIX下是区分大小写的] [命名规则: 1.有意义(英文,英文组合或英文缩写) 2.自己定

MySQL学习笔记_4_MySQL创建数据表(下)

 MySQL创建数据表(下) 五.数据表类型及存储位置 1.MySQL与大多数数据库不同,MySQL有一个存储引擎概念.MySQL可以针对不同的存储需求选择不同的存储引擎. 2. showengines; #查看MySQL所支持的存储引擎storageengine 3. showvariables like 'table_type'; #查看默认数据表类型 MyISAM和InnoDB最常用的存储引擎[表类型] 4.指定表类型[使用哪一个存储引擎]: createtable ...() eng

MySQL学习笔记之五 有关数据表操作

MySQL在创建表的时候,创建一个.frm文件保存表和列定义.索引存储在一个有.MYI(MYindex)扩展名的文件并且数据存储在有.MYD(MYData)扩展名的文件中.   一.用SHOW/ DESCRIBE语句显示数据表的信息 语法: SHOW TABLES [FROM db_name] [LIKE wild] or SHOW COLUMNS FROM tbl_name [FROM db_name] [LIKE wild] or SHOW INDEX FROM tbl_name [FROM

MySQL学习笔记-数据类型与操作数据表

MySQL学习笔记-数据类型与操作数据表 数据类型:  1.字符型  2.整型  3.浮点型  4.日期时间型 数据表操作:  1.插入记录  2.查找记录 记录操作:  1.创建数据表  2.约束的使用 1.数据类型 [1]整型: 数据类型 存储范围 字节 TINYINT 有符号型:-128~127(-2^7~2^7 -1),无符号型0~255(0~2^8 -1) 1 SMALLINT 有符号型:-2^15~2^15 -1,无符号型0~2^16 -1 2 MEDIUMINT 有符号型:-2^2

MySQL学习11:修改数据表(一)

修改数据表包括添加列.删除列.添加约束.删除约束,修改列定义和修改数据表名称,后面的两个我们使用时 一定要谨慎,尽量不使用. 下面就来一个一个单独介绍怎么修改数据表: 一添加数据表中的列 (1)添加单列 MySQL数据库的数据表中添加单列的语法格式为: ALTER TABLE table_name ADD [COLUMN] col_name columns_definition [FIRST | AFTER col_name]; 例子: SHOW COLUMNS FROM users1; ALT

Mysql DBA 高级运维学习笔记-DML之修改表中的数据实战

9.10 修改表中的数据 9.10.1 修改表中指定条件固定列的数据 1.命令语法:update 表名 set 字段=新值,-.where 条件(一定要注意条件) 2.修改指定的行字段的内容 a.查看要修改的表 [email protected] 02:3907->select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | wwnwan| | 2 | zbf | | 3 | lisi | | 4 |

MySQL学习笔记之七:数据的备份和恢复

我们知道,数据是一个企业IT架构的核心,为了防止因某些意外原因造成数据遗失或其它一些特殊目的,在平时对数据做好备份尤其重要. 一.为什么要备份 1.灾难恢复:硬件故障.软件故障.自然灾害.黑客攻击.误操作等 2.审计:有时需要知道数据在过去某个时间点是什么样的 3.测试:一个最简单的其于实际数据来测试的方法是,定期用最新的生产环境数据更新测试服务器,只要把备份文件还原到测试服务器即可 二.备份和恢复需要注意的要点 1.可容忍丢失多少数据 2.恢复需要在多长时间内完成 3.需要恢复什么 三.备份类

MySQL学习笔记02_数据库和表的基本操作

02_1 操作数据库 (1)创建数据库 CREATE DATABASE [IF NOT EXISTS] db_name [create_specification[, create_specification]...] 解释: [IF NOT EXISTS]创建时提前检查一下是否存在数据库 create_specification:(创建条件) [DEFAULT] CHARACTER SET charset_name | [DEFAULT] COLLATE collation_name CHAR

MySQL 使用while语句向数据表中批量插入数据

1.创建一张数据表 mysql> create table test_while ( -> id int primary key) charset = utf8; Query OK, 0 rows affected (0.28 sec) 查看数据表的结构 mysql> desc test_while; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Ext