SQL刷题笔记day8——SQL进阶——表与索引操作

目录

[1 创建一张新表](#1 创建一张新表)

[2 修改表](#2 修改表)

[3 删除表](#3 删除表)

[4 创建索引](#4 创建索引)

[5 删除索引](#5 删除索引)

1 创建一张新表

我的答案

复制代码
create table if not exists user_info_vip
(id int(11) primary key auto_increment Comment'自增ID', # 有了主键就不用写not nul了
uid	int(11) unique not null Comment'用户ID',
nick_name varchar(64) Comment'昵称',
achievement	int(11) default 0 Comment'成就值',
level int(11) Comment'用户等级',
job varchar(32) Comment'职业方向',
register_time datetime default CURRENT_TIMESTAMP Comment'注册时间')
default charset = utf8

正确答案:

复制代码
CREATE TABLE user_info_vip(
    id int(11) primary key auto_increment comment "自增ID",
    uid int(11) unique not null comment "用户ID",
    nick_name varchar(64) comment "昵称",
    achievement int(11) default 0 comment "成就值",
    level int(11) comment "用户等级",
    job varchar(32) comment "职业方向",
    register_time datetime default current_timestamp comment "注册时间"
)DEFAULT CHARSET=UTF8;

复盘:有了主键就不用写not nul了

复制代码
--创建新表,如果存在则覆盖
drop table [if exists] 表名;
--创建新表,如果存在则返回
create table
[if not exists] 表名 -- 不存在才创建,存在就跳过
(<列名1> <数据类型> -- 列名和类型必选
  [ primary key -- 可选的约束,主键
   | foreign key  -- 外键,引用其他表的键值
   | auto_increment -- 自增ID
   | comment <注释> -- 列注释(评论)
   | default <值> -- 默认值
   | unique -- 唯一性约束,不允许两条记录该列值相同
   | not null -- 该列非空,输入空会报错
   | current_timestamp -- 当前时间戳
  ], ...
) [character set <字符集名>] -- 字符集编码
[collate <校对规则名>] -- 列排序和比较时的规则(是否区分大小写等)

2 修改表

修改方法:

复制代码
alter table 表名 修改选项;
选项集合:
{ add column <新增列名> <类型> after <某一列>  -- 在某一列之后增加列
| change column <旧列名> <新列名> <新列类型> -- 修改列名或类型
| alter column <列名> { set default <默认值> | drop default } -- 修改/删除 列的默认值
| modify column <列名> <类型> -- 修改列类型
| drop column <列名> -- 删除列
| rename to <新表名> -- 修改表名
| character set <字符集名> -- 修改字符集
| collate <校对规则名> } -- 修改校对规则(比较和排序时用到,是否区分大小写等)

我的代码:表名后面不需要大花括号{ }

复制代码
alter table user_info
add column school varchar(15) after level,
change column job profession varchar(10),
modify column achievement int(11) default 0;

修改三次的方法代码:

复制代码
ALTER TABLE user_info ADD column school varchar(15) AFTER LEVEL;
ALTER table user_info CHANGE column job profession varchar(10);
ALTER TABLE user_info MODIFY column achievement int(11) default 0;

3 删除表

复制代码
drop table [if exists] 表名 [,表名1,...];

我的代码:天真了,删除表能使用的很有限?

复制代码
drop table if exists exam_record
where YEAR between 'exam_record_2011' and 'exam_record_2014'

正确代码:暴力删除

复制代码
drop table if exists 
    exam_record_2011, 
    exam_record_2012,
    exam_record_2013,
    exam_record_2014

或者一个一个的删除:

复制代码
drop table if exists exam_record_2011;
drop table if exists exam_record_2012;
drop table if exists exam_record_2013;
drop table if exists exam_record_2014;

4 创建索引

方法一、使用create index

  • 使用\]:`create [unique|fulltext] index 索引名 on 表名 (列名);`

方法二、修改表的方式创建索引

  • 使用\]:`alter table examination_info add [索引类型] index 索引名(列名);`

方法三、建表的时候创建索引

  • 使用\]:`create table tableName( id int not null, 列名 列的类型, [索引类型] index [索引名] (列名,...););`

    create index idx_duration on duration;
    create unique index uniq_idx_exam_id on exam_id;
    create fulltext index full_idx_tag on tag;

正确修改后:补全表名

复制代码
# 创建普通索引
create index idx_duration on examination_info(duration);
# 创建唯一性索引
create unique index uniq_idx_exam_id on examination_info(exam_id);
# 创建全文索引
create fulltext index full_idx_tag on examination_info(tag);

方法二:修改表的方式创建索引

复制代码
-- 唯一索引
ALTER TABLE examination_info
ADD UNIQUE INDEX uniq_idx_exam_id(exam_id);
-- 全文索引
ALTER TABLE examination_info
ADD FULLTEXT INDEX full_idx_tag(tag);
-- 普通索引
ALTER TABLE examination_info
ADD INDEX idx_duration(duration);

学习链接:https://blog.csdn.net/chengyj0505/article/details/128376127?spm=1001.2014.3001.5502

5 删除索引

我的错误粗暴代码:drop [unique|fulltext]index index_name on table_name ;

复制代码
drop unique index uniq_idx_exam_id on examination_info;
drop fulltext index full_idx_tag on examination_info;

删除不需要给出类型[unique|fulltext]

修改后

法一:drop index index_name on table_name ;

复制代码
drop index uniq_idx_exam_id on examination_info;
drop index full_idx_tag on examination_info;

法二:alter table table_name drop index index_name ;

注意:table不能少

复制代码
alter table examination_info drop index uniq_idx_exam_id;
alter table examination_info drop index full_idx_tag; #table不能少
相关推荐
moxiaoran575321 分钟前
uni-app学习笔记二十三--交互反馈showToast用法
笔记·学习·uni-app
Zfox_3 小时前
Redis:Hash数据类型
服务器·数据库·redis·缓存·微服务·哈希算法
陈丹阳(滁州学院)5 小时前
若依添加添加监听容器配置(删除键,键过期)
数据库·oracle
远方16096 小时前
14-Oracle 23ai Vector Search 向量索引和混合索引-实操
数据库·ai·oracle
GUIQU.7 小时前
【Oracle】数据仓库
数据库·oracle
scdifsn7 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
恰薯条的屑海鸥8 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
咖啡啡不加糖8 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
曼汐 .8 小时前
数据库管理与高可用-MySQL高可用
数据库·mysql
2301_793102498 小时前
Linux——MySql数据库
linux·数据库