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倚,靠;
可以明显的查询速度!
MySQL学习笔记_3_MySQL创建数据表(中)的更多相关文章
- MySQL学习笔记_2_MySQL创建数据表(上)
MySQL创建数据表(上) 一.创建数据表的SQL语句模型[弱类型] CREATETABLE [IF NOT EXISTS] 表名称( 字段名1列的类型[属性][索引], 字段名2 列的类型[属性][ ...
- MySQL学习笔记_4_MySQL创建数据表(下)
MySQL创建数据表(下) 五.数据表类型及存储位置 1.MySQL与大多数数据库不同,MySQL有一个存储引擎概念.MySQL可以针对不同的存储需求选择不同的存储引擎. 2. showengines ...
- MySQL学习(一) 数据表基本操作
创建数据库:create database db_name 查看数据库结构:show create database db_name 删除数据库:drop database db_name 查看数据库 ...
- mysql学习(八)数据表类型-字符集
数据存储引擎: MyISAM:强化快速读取操作. 也有缺点.一些功能不支持 InnoDB:支持一些MyIASM一些不支持的功能 缺点:占用空间大 对比 ...
- MySQL学习(三): 初识数据表
打开数据库: USE db_name : 打开数据库. 创建数据表: 查看数据表: 查看数据表结构: 数据简单的插入与查找: 插入:INSERT [INTO] tbl_name [(col_name) ...
- MySQL学习笔记02_数据库和表的基本操作
02_1 操作数据库 (1)创建数据库 CREATE DATABASE [IF NOT EXISTS] db_name [create_specification[, create_specifica ...
- MySQL学习笔记十一:数据导入与导出
数据导入 1.mysqlimport命令行导入数据 在使用mysqlimport命令导入数据时,数据来源文件名要和目标表一致,不想改文件名的话,可以复制一份创建临时文件,示例如下. 建立一个文本use ...
- MySQL 使用while语句向数据表中批量插入数据
1.创建一张数据表 mysql> create table test_while ( -> id int primary key) charset = utf8; Query OK, ro ...
- mysql for Mac 下创建数据表中文显示为?的解决方法
在我的绝版Mac mini下安装了mysql 5.7版本,实例中,在通过load data 导入数据时发现表中的中文显示为 ? 通过百度,发现多个版本的解决方法,将其中一个成功解决的方法贴上来: 大 ...
随机推荐
- 2018 dnc 公司案例大全,迎接.NET Core开源新时代
2018 dnc 公司案例大全,迎接.NET Core开源新时代 dnc = .NET Core.dotnet Core dnc是微软新一代主力编程平台,开源.免费.跨平台.轻量级.高性能,支持L ...
- 克拉默法则(Cramer's Rule)的证明
克拉默法则: 先说一下为什么要写这个,作为一个大一新生,必须要学的就包括了线性代数,而且线性代数等数学知识对计算机专业也有很大帮助.但是在学习过程中遇到一个讲解的不清楚的知识点(Cramer's Ru ...
- Python列表函数&方法
Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表元素最大值 4 min(list)返 ...
- Android自定义View实战(SlideTab-可滑动的选择器)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52178553 本文出自:[openXu的博客] 目录: 初步分析重写onDraw绘制 重写o ...
- android混淆那些坑
ProGuard简介 在最新的Android Studio 2.2.2版本创建的Android工程中,module中的build.gradle有如下一段配置.这里的minifyEnabled即用来控制 ...
- 21 PagerTabStrip-PagerTitleStrip-viewPager
PagerTabStrip:可以点击跳转到对应viewPager界面 PagerTitleStrip:不可点击 在eclipse开发时如果目标版本为API23那么会有不显示的问题 解决:更换v4包 解 ...
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required
在Android 6.0(API 23)中,Google已经移除了移除了Apache HttpClient相关的类.HttpResponse类.缺失jar包使用HttpResponse等会报错: Th ...
- SSH网上商城---商品详情页的制作
在前面的博文中,小编分别简单的介绍了邮件的发送以及邮件的激活,逛淘宝的小伙伴都有这样的体会,比如在搜索框中输入连衣裙这个商品的时候,会出现多种多样各种款式的连衣裙,连衣裙的信息包括价格,多少人购买,商 ...
- Android必知必会-Stetho调试工具
一.背景 Stetho是 Facebook 出品的一个强大的 Android 调试工具,使用该工具你可以在 Chrome Developer Tools查看APP的布局, 网络请求(仅限使用Volle ...
- Android的ToggleButton和Switch以及AnalogColok和DigitalColok的用法-android学习之旅(二十)
ToggleButton 和Switch简介 ToggleButton 和Switch都是继承了Button,所以他们的属性设置和Button差不多. 分别支持的属性 ToggleButton 的属性 ...