MySQL数据库入门--读书笔记(二)

第二章

创建数据库:create database itcast;

查询数据库:show databases;

查看创建好的数据库信息:show create database itcast;

修改数据库:alter database 数据库名称 default character set 编码方式 collate 编码方式_bin;

例: alter database itcast default character set gbk collate gbk_bin;

删除数据库:drop database 数据库名称;【drop database itcast;】

数据类型:

整数类型:tinyint、samllint、mediumint、int、bigint

浮点数类型和定点数类型:float、double、decimal(m,d)【m表示数据的长度,d表示小数点后的长度】

日期和时间类型:year、date、time、datetime、timestamp【简化插入时,1~69为2001~2069,70~99为1970~1999】

字符串和二进制类型:char、varchar(可变长度字符串)、binary、varbinary(可变长度二进制数据)、bolb(二进制大数据)、text(大文本数据)、enum、set(字符串对象)、bit(字段类型)

创建数据表:

         createdatabase itcast;

         useitcast;

         createtable tb_grade ( id int(11), name varchar(20), grade float );

         showtables;

查看数据表(建表语句):show create table tb_grade;【show create tabletb_grade\G更加整洁】

查看数据表(表格形式):desc tb_grade;【describe tb_grade;】【null表示该列是否可以存储null值;key是否已经编制索引;default是否有默认值;extra表示获取到的与给定列相关的附加信息】

修改数据表:alter table 旧的表名 rename [to] 新的表名;【to可加可不加】

修改字段名:alter table grade change name username vatrchar(20);【alter table 表名 change 旧的字段名 新的字段名 新数据类型】

修改字段的数据类型:alter table grade modify id int(20);

添加字段:alter table grade add age int(11) [约束条件] [after已存在的字段名];【first放在第一个,after 已存在的字段名 放在已存在的字段名后面】

删除字段:alter table grade drop age;

修改字段的排列位置:alter table grade modify 字段名1 数据类型first|after 字段名2

表的约束:

         primarykey:主键约束,用于唯一标识对应的记录

         foreignkey:外键约束

         notnull:非空约束

         unique:唯一性约束

         default:默认值约束,用于设置字段的默认值

主键约束:

单字段主键:create tableexample1(id int primary,name varchar(20),grade float);

多字段主键:create tableexample2(stu_id int,course_id int,grade float,primary key(stu_id,course_id));

非空约束:create table example4(id int primary key,name varchar(20) notnull,grade float);

唯一约束:create table example5(id int primary key,stu_id int unique,name notnull);

默认约束:create table example6(id int primary key,stu_id int unique,gradefloat default 0);

设置表的字段值自动增加:create table example7(id int primary key auto_increment,stu_id intunique,grade float);

创建索引:

         Createtable 表名(字段名数据类型 [完整性约束条件]

字段名 数据类型 [完整性约束条件]

字段名 数据类型 [unique][fulltext] [spatial] index|key

[别名] (字段名1 [(长度)]) [asc|desc]);

uniqiue:可选参数,表示唯一索引

         fulltext:可选参数,表示唯一索引

         spatial:可选参数,表示空间索引

         index|key:用来表示字段的索引,二者选一即可

         别名:可选参数,表示创建的索引的名称

         字段名1:指定索引对应字段的,名称

         长度:可选参数,用于表示索引的长度

         asc|desc:可选参数,asc表示升序排列,desc表示降序排列

创建普通索引:create table t1(id int,name varchar(20),score float,index(id));【可用explainselect * from t1 while id=1 \G查看索引是否被使用】

创建唯一性索引:create table t2(id int not null, name varchar(20) not null,scorefloat,unique index unique_id(id asc));

创建全文索引:create table t3(id int not null,name varchar(20) not null,scorefloat,fulltext index fulltext_name(name))engine=myisam;【目前只有myisan存储引擎支持全文索引,innodb存储引擎还不支持】

创建单列索引:createb table t4(id int not null,name varchar(20) not null,scorefloat,index single_name(name(20)));

创建多列索引:create table t5(id int not null,name varchar(20) not null,scorefloat,index multi(id name(20)));【只有使用这些字段中的第一个字段,多列索引才会被使用eaplain select * from t5 where id=1\G和explain select *from t5 where name=’Mike’\G比较】

创建空间索引:create table t7(space geometry not null,spatial indexsp(space))engine=myisam;

使用create index在已存在的表上创建索引:create [unique|fulltext|spatial] index 索引名 on 表名 [字段名 [(长度)][asc|desc]]

         创建一个book表使用:createtable book(bookid int not null,bookname varchar(255) not null,authorsvarchar(255) not null,info varchar(255) null,comment varchar(255)null,publicyear year not null);

         创建普通索引:createindex index_id on book(bookid);

         创建唯一性索引:createunique index uniqueidx on book(bookid);

         创建单列索引:createindex singleidx on book(bookid);

         创建多列索引:createindex multiidxon book(aurhors(20),info(20));

         创建全文索引:

1、 删除book表

2、重建book表:create table book(bookid int not null,bookname varchar(255) notnull,authors varchar(255) not null,info varchar(255) null,comment varchar(255)null,publicyear year not null)engine=myisam;

                  3、createfulltext index fulltextidx on book(info);

         创建空间索引:

1、 新建数据t8:createtable t8(space geometry not null)engine=myisam;

2、 create spatial index spatialidx on t8(space);

使用alter table在已存在的表上创建索引:

         altertable 表名 add [unique|fulltext|spatial] index 索引名 (字段名 [(长度)][asc|desc])

         重建book表:

         创建普通索引:altertable book add index index_id(bookid);

         创建唯一性索引:altertable book add unique index uniqueiex(bookid);

         创建单列索引:altertable book add index singleidx(comment(50));

         创建多列索引:altertable book add index multiidx(authors(20),info(50));

         创建全文索引:

1、 删除book表

2、 重建book表:createtable book(bookid int not null,bookname varchar(255) not null,authorsvarchar(255) not null,info varchar(255) null,comment varchar(255)null,publicyear year not null)engine=myisam;

3、 Alter table book add fulltext index fulltextidx(info);

创建空间索引:

1、 创建空间表t9:createtable t9(space geometry not null)engine=myisam;

2、 alter table t9 add spatial index spatialidx(space);

删除索引:

         使用alter table删除索引:altertable 表名 drop index 索引名

                                                              altertable book drop index fulltextidx;

         使用drop index删除索引:dropindex 索引名 on 表名

                                                             dropindex t9 on spatialidx;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值