当前位置: 首页 > ops >正文

修改一个表的相关操作语句

1.创建例子

create table teacher(teacher_no char(10) primary key,   #主键约束teacher_name char(10) not null,	#非空约束teacher_contact char(20) not null	
)engine=InnoDB default charset =gbk;	#存储引擎和字符集设置
create table classes(class_no int auto_increment primary key;	#自增class_name char(20) not null unique;department_name char(20) not null
)engine=InnoDB default charset = gbk;
create table course(course_no int auto_increment primary key,course_name char(10) not null,up_limit int default 60,	#默认值约束description text not null,status char(6) default '未审核',teacher_no char(10) not null unique,constraint course_teacher_fk foreign key(teacher_no) references teacher(teacher_no)	#外键约束
)engine=Innodb default charset=gbk;
create table student(student_no char(11) primary key,student_name char(10) not null,student_contact char(20) not null,class_no int,constraint student_class_fk foreign key(class_no) references classes(class_no)	#外键约束
)engine=Innodb default charset=gbk;
create table choose(choose_no int auto_increment primary key,student_no char(11) not null,course_no int not null,score tinyint unsigned,choose_time datetime not null,constraint choose_student_fk foreign key(student_no) references student(student_no),constraint choose_course_fk foreign key(course_no) references course(course_no)
)engine=Innodb default charset=gbk;

2.复制一个表结构

(1)create table tab2 like tab1;
将tab1结构拷贝到tab2
(2)create table tab2 select * from tab1;
将tab1的结构和记录都拷贝到tab2
(3)create table tab2 select * from tab1 where 1=2;
将tab1的结构复制到tab2

3.表结构改动

(1)alter table tab1 drop col1;
删除字段
(2)alter table tab1 add col2 数据类型【约束条件】[first|after col1];
添加字段
(3)alter table tab1 change col1 col2 char(20)
修改字段名(或者数据类型)
(4)alter table tab1 modify col1 char(30);
仅修改字段名
等同于alter table tab1 change col1 col1 char(30);

4.修改约束条件

(1)alter table tab1 add constraint 约束名 约束类型(字段名)
添加约束条件
(2)删除约束条件
alter table tab1 drop primary key
(3)删除外键约束
alter table tab1 drop foreign key 约束名
(4)删除唯一性约束
alter table tab1 drop index 唯一索引名
(5)删除非空限制
alter table tab1 modify col1 datatype NULL;
datatype是原始列的数据类型

5.修改表的其他选项

alter table tab1 engine=新的存储引擎类型
alter table tab1 default charset=新的字符集
alter table tab1 auto_increment=新的初始值
alter table tab1 pack_keys=新的压缩类型

6.修改表名

(1)rename table 旧表名 to 新表名
(2)alter table 旧表名 rename 新表名

7.删除表

drop table 表名;

8.索引

索引分为聚簇索引,主索引,唯一性索引,普通索引,复合索引等。
MYSQL还支持全文索引,当查询数据量大的字符串信息时,使用全文索引可以大幅提升字符串的检索效率。需要注意的是,全文索引只能创建char,varchar或者text字符串类型上。
(1)创建表时新建索引

create table book(isbn char(20) primary key,name char(100) not null,brief_introduction text not null,price decimal(6,2),publish_time date not null,unique index isbn_unique(isbn),index name_index(name(20)),fulltext index brief_fulltext(name, brief_introduction),index complex_index(price, publish_time)
)engine=MYISAM default charset=gbk;

(2)在已有表上加索引
create [unique|fulltext] index 索引名 on 表名(字段名[(长度)]);
alter table 表名 add [unique | fulltext] index 索引名 (字段名[(长度)]);

9.删除索引

drop index 索引名 on 表名

10.表记录的更新

(1)insert into 表名 values(val1,val2,val3,…);
(2)insert into 表名(列名1,列名2,列名3,…) values(val1,val2,val3,…);
(3)批量插入:
insert into 表名 values
(val1,val2,val3,…),
(val1,val2,val3,…),
(val1,val2,val3,…),
(val1,val2,val3,…);
(4)从另一个表的记录取出来插入新表
insert into tab1 select * from tab2;
(5)使用replace插入新纪录
replace into 表名[(字段列表)] values(值列表);
replace into 目标表名[(字段列表1)] select (字段列表2) from 源表 where 表达式;
replace into 表名 set 字段1=值1,字段2=值2
注意:replace需要慎用,如果新记录的主键值或者唯一性约束的字段值与旧纪录相同

11.表记录修改

update 表名 set 字段名1=值1,字段名2=值2,…,字段名n=值n where 表达式;

12.表记录删除

(1)delete from 表名 where 表达式;
(2)完全清空一个表
truncate table 表名;
从逻辑上说,该语句与"delete from 表名"语句的作用相同,但是在某些情况下,两者在使用上有所区别。例如,如果清空记录的表是父表,那么truncate命令将永远执行失败。如果使用truncate table 成功清空表记录,那么会重新设置自增型字段的计数器,而delete不会重置计数器的起点。
truncate table 语句不支持事务的回滚,并且不会出发触发器程序的运行。

http://www.xdnf.cn/news/6444.html

相关文章:

  • “DiT和Flux”与“Stable Diffusion”两种不同的生成模型范式
  • Vue中的自定义指令适用于哪些场景
  • 如何在 Windows 命令提示符中创建多个文件夹和多个文件
  • Python3 简易DNS服务器实现
  • redis持久化方式
  • buildroot使用外部编译链编译bluez蓝牙工具
  • 沃伦森智能无功补偿系统解决电力电容器频繁投切的隐患
  • 前端代码生成博客封面图片
  • Spring-messaging-MessageChannel的子接口PollableChannel
  • 软考软件评测师——计算机组成与体系结构
  • 学习日志07 java
  • 登录接口中图片验证码Tesseract-OCR识别Java脚本
  • 全息美AISEO引领AIGEO新趋势
  • centos7部署mysql5.7
  • C++ 函数声明,定义与命名空间的关系
  • 投影仪基础知识及选购方向小记②
  • Media Controller API 1. Introduction 翻译
  • 某某查响应数据解密逆向分析-js逆向
  • 边缘计算平台
  • 串行接口与并行接口
  • API 接口开放平台 Crabc 3.2 发布
  • 70、微服务保姆教程(十三)Docker容器详细讲义
  • 香港科技大学广州|智能制造学域硕博招生宣讲会-西北工业大学专场
  • 【沉浸式求职学习day40】【java面试题精选2】
  • 代码分支操作步骤
  • 人工智能(AI)与机器学习(ML):定义、区别及应用解析
  • web3 前端常见错误类型以及错误捕获处理
  • 四维时空数据安全传输新框架:压缩感知与几何驱动跳频
  • 游戏代码混淆的作用与应用分析
  • C++:运算符重载