当前位置:网站首页>【数据库】表的查看、修改和删除
【数据库】表的查看、修改和删除
2022-04-23 04:46:00 【明璐花生牛奶】
练习二 表的查看、修改和删除
一、表结构的修改
1、为student表格增加籍贯列jiguan,数据类型varchar,字段宽度为20字节,不允许输入空值。
语法:ALTER TABLE table_name ADD 属性名 属性类型 NOT NULL;
命令如下:
修改的话:alter table table_name modify 属性名 属性类型 not null;
2、重命名student表的籍贯列“jiguan”为“jg”。
语法:ALTER TABLE table_name CHANGE 旧属性名 新属性名 旧数据类型;
命令如下;
3、删除student表中的jg字段。
语法:ALTER TABLE table_name DROP 属性名;
命令如下:
4、将student表中的sdept字段长度改为20
语法:ALTER TABLE table_name MODIFY 属性名 数据类型;
--数据类型为修改后的数据类型
命令如下:
5、在sno字段上为Student表设置主键。
语法:ALTER TABLE table_name ADD PRIMARY key 属性名;
命令如下:
6、在Cno字段上为Course表设置主键。
命令如下:
7、为SC表的Sno和Cno设置外键。
命令如下:
首先显示一下三个表:
添加外键的操作:
添加外键之后的sc表:
字段Sno和字段Cno的key位置上出现了MUL的字样
8、删除键(如有外键的话,要先删除外键,再删除主键)
一、如何删除外键?
分布删除外键:
第一步:先删除外键:
alter table table_name drop foreign key 外键名;
第二步:再删除索引:
alter table table_name drop index 外键名;
/*但是注意一点:MySQL不支持直接删除外键:alter table table_name drop constraint 外键名;*/
我发现alter table table_name drop constraint 外键名;和alter table table_name drop foreign key 外键名;起到的作用是一样的
And
------------------------------------其实到这一步就已经删除外键了(如果再次添加外键也是可以的)
如果要完全删除这个键的话就需要
alter table table_name drop index 外键名;
而且我查看sc表上的Key发现就没有MUL
关于删除外键约束需要注意的几点
* 从表的外键通常为主表的主键(就像这里的sc表的Sno是student的主键、Cno是course的主键)
* 从表里外键的数据类型必须与主表中主键的数据类型一致
* 主表发生变化时应注意主表与从表的数据的一致性问题
- 如何删除主键?
alter table table_name drop primary key;//因为主键唯一所以删除的对象是表格
对于有自定义名字的主键:
添加主键:alter table table_name add constraint p_pk primary key(Sname);//其中p_pk是这个主键的名字,就像外键一样。
删除主键:alter table table_name drop constraint p_pk;
二、数据操作
1、使用insert-SQL命令向上述表中输入如下数据记录。
学生关系表Student
Sno |
Sname |
Ssex |
Sbirthday |
Sdept |
Speciality |
20180101 |
李勇 |
男 |
2000-01-12 |
CS |
计算机应用 |
20180201 |
刘晨 |
女 |
2001-06-04 |
IS |
电子商务 |
20180301 |
王敏 |
女 |
2002-12-23 |
MA |
数学 |
20180202 |
张立 |
男 |
2001-08-25 |
IS |
电子商务 |
例:insert into student(sno,sname) values('20180101','李勇');
命令如下:
Sno |
Cno |
Degree |
20180101 |
C01 |
92 |
20180101 |
C02 |
85 |
20180101 |
C03 |
88 |
20180201 |
C02 |
90 |
20180201 |
C03 |
80 |
20180301 |
C01 |
89 |
20180301 |
C02 |
87 |
20180202 |
C01 |
87 |
课程关系表Course 成绩表SC
Cno |
Cname |
C01 |
数据库 |
C02 |
数学 |
C03 |
信息系统 |
C04 |
操作系统 |
命令如下:
课程关系表Course:
成绩表:
刚开始我发现初始的decimal精度太小,输入的值会越界
然后我对Degree的精度作了一些修改:
就可以正常的输入了
2、使用update命令将SC表C02课程的Degree字段值全部加5分。
命令如下:
3、删除SC表中C02课程的全部记录。
命令如下:
4、查看SC表格中所有数据。
命令如下:
- 思考题
1、MySQL的数据库文件有几种?扩展名分别是什么?
种类有很多,这里我找到一篇blog介绍数据库文件,很详细
MySQL数据库文件 - DB-Engineer - 博客园 (cnblogs.com)
2、如何实现数据库的备份和还原?
教你如何进行数据库的备份与恢复_zimao_的博客-CSDN博客_数据库备份与恢复
3、在定义基本表语句时,NOT NULL参数的作用是什么?
该属性的值不可以为空值
4、主码可以建立在“值可以为NULL”的列上吗?
不可以
版权声明
本文为[明璐花生牛奶]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_54438368/article/details/124264453
边栏推荐
- Code007 -- determine whether the string in parentheses matches
- 解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
- What is the meaning of load balancing
- Spark optimization
- Leetcode009 -- search the target value in the array with binary search
- QML advanced (IV) - drawing custom controls
- SQL statement for adding columns in MySQL table
- /etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
- Progress of innovation training (III)
- Innovation training (VI) routing
猜你喜欢
229. Find mode II
Excel protects worksheets and workbooks from damage
Wechat payment function
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Windows remote connection to redis
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Eight misunderstandings that should be avoided in data visualization
Key points of AWS eks deployment and differences between console and eksctl creation
随机推荐
AWS eks add cluster user or Iam role
Basic use of shell WC (counting the number of characters)
MySQL time function query
Better way to read configuration files than properties
Spark small case - RDD, spark SQL
Leetcode002 -- inverts the numeric portion of a signed integer
Innovation training (VII) FBV view & CBV view
POI export message list (including pictures)
Getprop property
Eksctl deploying AWS eks
C language: spoof games
leetcode009--用二分查找在数组中搜索目标值
ApplicationContext injection bean
Spark small case - RDD, broadcast
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
Summary of MySQL de duplication methods
Flink case - Kafka, MySQL source
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Spark optimization
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘