Mysql更新,删除与索引(五)

一、更新数据

mysql> select *from fruits where s_id=103;
+——+——+——–+———+
| f_id | s_id | f_name | f_price |
+——+——+——–+———+
| a2 | 103 | banana | 4.1 |
+——+——+——–+———+
1 row in set

//修改s_id等于103的f_name与f_price
mysql> update fruits set f_name=’organge’,f_price=5.2 where s_id=103;
Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select *from fruits where s_id=103;
+——+——+———+———+
| f_id | s_id | f_name | f_price |
+——+——+———+———+
| a2 | 103 | organge | 5.2 |
+——+——+———+———+
1 row in set

二、删除数据

mysql> delete from fruits where s_id=102;
Query OK, 0 rows affected

mysql> select *from fruits;
+——+——+————+———+
| f_id | s_id | f_name | f_price |
+——+——+————+———+
| a1 | 101 | apple | 5.2 |
| a2 | 103 | organge | 5.2 |
| b1 | 101 | blackberry | 10.2 |
| b3 | 104 | melon | 1.4 |
| b4 | 106 | Xxx | 1.4 |
+——+——+————+———+
5 rows in set

三、索引

索引是对数据库表中一列或者多列的值进行排序的一种结构,可以快速的查找出特定行的值
1.普通索引与唯一索引(普通索引允许纯插入重复值与空值;唯一索引索引列必须唯一,允许有空值)

创建普通索引
mysql> explain select *from book where year_publication=1990 ;
+—-+————-+——-+————+——+——————+——————+———+——-+——+———-+———————–+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+——-+————+——+——————+——————+———+——-+——+———-+———————–+
| 1 | SIMPLE | book | NULL | ref | year_publication | year_publication | 1 | const | 1 | 100 | Using index condition |
+—-+————-+——-+————+——+——————+——————+———+——-+——+———-+———————–+
1 row in set

创建唯一索引

mysql> create table t1(id int not null,
name char(30) not null,
unique index uniqIdx(id));
Query OK, 0 rows affected

mysql> show create table t1;
+——-+——————————————————————————————————————————————-+
| Table | Create Table |
+——-+——————————————————————————————————————————————-+
| t1 | CREATE TABLE t1 (
id int(11) NOT NULL,
name char(30) NOT NULL,
UNIQUE KEY uniqIdx (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+——-+——————————————————————————————————————————————-+
1 row in set

创建单例索引

index SingleIdx(name(20))

创建组合索引

index MultiIdx(id,name,age(100))

创建全文索引

fulltext index fulltxtIdx(info) Engine=MyISAM;

创建空间索引

(g GEOMETRY not null,SPATIAL index spatIdx(g))ENGINE=MyISAM;

在已存在的表上创建索引

建立UniqidIdx的唯一索引:alter table book add unique index UniqidIdx(bookId);

建立comment的单列索引:alter table book add index BkcmtIdx(comment(50));

建立组合索引:alter table book add index BkAuAndInfoIdx(author(30),info(50));

建立名称为spatIdx的空间索引:alter table t7 add SPATIAL index spatIdx(g);

四、删除索引

使用alter table 或者drop index语句

alter table book drop index UniqidIdx;

drop index BkAuAndInfoIdx on book;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值