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

2024-06-03 00:36

本文主要是介绍SQL刷题笔记day8——SQL进阶——表与索引操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1 创建一张新表

2 修改表

3 删除表

4 创建索引

 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 表名 (列名);
  • [注意]:unique 唯一性索引、fulltext 全文索引

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

  • [使用]:alter table examination_info add [索引类型] index 索引名(列名);
  • [注意]:索引类型:普通索引、唯一性索引、全文索引

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

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

我的错误代码:方法一 使用create 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不能少

这篇关于SQL刷题笔记day8——SQL进阶——表与索引操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1025540

相关文章

Oracle查询表结构建表语句索引等方式

《Oracle查询表结构建表语句索引等方式》使用USER_TAB_COLUMNS查询表结构可避免系统隐藏字段(如LISTUSER的CLOB与VARCHAR2同名字段),这些字段可能为dbms_lob.... 目录oracle查询表结构建表语句索引1.用“USER_TAB_COLUMNS”查询表结构2.用“a

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Python操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali