当前位置:网站首页>View, modify and delete [database] table
View, modify and delete [database] table
2022-04-23 04:52:00 【Minglu peanut milk】
Exercise 2 View of table 、 Modification and deletion
One 、 Modification of table structure
1、 by student Add native place column to the table jiguan, data type varchar, The field width is 20 byte , Null values are not allowed .
grammar :ALTER TABLE table_name ADD Property name Attribute types NOT NULL;
The order is as follows :

If you change it :alter table table_name modify Property name Attribute types not null;
2、 rename student Native place column of table “jiguan” by “jg”.
grammar :ALTER TABLE table_name CHANGE Old attribute name New property name Old data types ;
The order is as follows ;

3、 Delete student In the table jg Field .
grammar :ALTER TABLE table_name DROP Property name ;
The order is as follows :

4、 take student In the table sdept Change the field length to 20
grammar :ALTER TABLE table_name MODIFY Property name data type ;
-- The data type is the modified data type
The order is as follows :

5、 stay sno The field is Student Table set primary key .
grammar :ALTER TABLE table_name ADD PRIMARY key Property name ;
The order is as follows :


6、 stay Cno The field is Course Table set primary key .
The order is as follows :

7、 by SC Tabular Sno and Cno Set up foreign keys .
The order is as follows :
First, let's show three tables :

Adding foreign keys :

After adding foreign keys sc surface :


Field Sno And field Cno Of key In the position MUL The words...
8、 Delete key ( If there is a foreign key , To delete a foreign key first , Delete the primary key )
One 、 How to delete foreign keys ?
Distribute delete foreign keys :
First step : Delete the foreign key first :
alter table table_name drop foreign key Foreign key name ;
The second step : Then delete the index :
alter table table_name drop index Foreign key name ;

/* But be careful :MySQL Direct deletion of foreign keys is not supported :alter table table_name drop constraint Foreign key name ;*/
I find alter table table_name drop constraint Foreign key name ; and alter table table_name drop foreign key Foreign key name ; The effect is the same

And

------------------------------------ In fact, foreign keys have been deleted at this step ( If you add foreign keys again, you can )
If you want to delete this key completely, you need
alter table table_name drop index Foreign key name ;

And I check sc On the table Key Found no MUL

Some points to note about deleting foreign key constraints
* The foreign key of the slave table is usually the primary key of the master table ( Like here sc Tabular Sno yes student Primary key of 、Cno yes course Primary key of )
* The data type of the foreign key in the slave table must be consistent with the data type of the primary key in the primary table
* When the master table changes, pay attention to the consistency of the data between the master table and the slave table
- How to delete a primary key ?
alter table table_name drop primary key;// Because the primary key is unique, the deleted object is the table
For primary keys with custom names :
Add primary key :alter table table_name add constraint p_pk primary key(Sname);// among p_pk Is the name of the primary key , Just like foreign keys .
Delete primary key :alter table table_name drop constraint p_pk;
Two 、 Data manipulation
1、 Use insert-SQL Command to enter the following data records into the above table .
Student relations form Student
| Sno |
Sname |
Ssex |
Sbirthday |
Sdept |
Speciality |
| 20180101 |
Li Yong |
male |
2000-01-12 |
CS |
Computer application |
| 20180201 |
Liu Chen |
Woman |
2001-06-04 |
IS |
Electronic Commerce |
| 20180301 |
Wang min. |
Woman |
2002-12-23 |
MA |
mathematics |
| 20180202 |
Zhang Li |
male |
2001-08-25 |
IS |
Electronic Commerce |
example :insert into student(sno,sname) values('20180101',' Li Yong ');
The order is as follows :

| 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 relationship table Course League tables SC
| Cno |
Cname |
| C01 |
database |
| C02 |
mathematics |
| C03 |
The information system |
| C04 |
operating system |
The order is as follows :
Course relationship table Course:

League tables :
At first I found the initial decimal Accuracy is too small , The value entered will be out of bounds
![]()
And then I was right Degree Some modifications have been made to the accuracy of :

You can input normally

2、 Use update The order will SC surface C02 Curriculum Degree Add all field values 5 branch .
The order is as follows :

3、 Delete SC In the table C02 All records of the course .
The order is as follows :

4、 see SC All the data in the table .
The order is as follows :

- Thinking questions
1、MySQL There are several kinds of database files ? What are the extensions ?
There are many kinds , Here I found an article blog Introduce database files , Very detailed
MySQL Database files - DB-Engineer - Blog Garden (cnblogs.com)

2、 How to backup and restore the database ?
3、 When defining basic table statements ,NOT NULL What is the function of parameters ?
The value of this property cannot be null
4、 The main code can be established in “ The value can be NULL” On the train ?
Can not be
版权声明
本文为[Minglu peanut milk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230446089195.html
边栏推荐
- Unity rawimage background seamlessly connected mobile
- leetcode001--返回和为target的数组元素的下标
- Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
- Manually write smart pointer shared_ PTR function
- Innovation training (XI) airline ticket crawling company information
- Better way to read configuration files than properties
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- Leetcode - > 1 sum of two numbers
- 数据孤岛是什么?为什么2022年仍然存在数据孤岛?
- [WinUI3]編寫一個仿Explorer文件管理器
猜你喜欢

Spark FAQ sorting - must see before interview

Introduction to raspberry pie 3B - system installation

View analysis of scenic spots in ArcGIS

Pixel mobile phone brick rescue tutorial

Details related to fingerprint payment

New terminal play method: script guidance independent of technology stack

Innovation training (IV) preliminary preparation - server
![Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]](/img/99/095063b72390adea6250f7b760d78c.png)
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]

跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?

Spark small case - RDD, spark SQL
随机推荐
Teach you how to build the ruoyi system by Tencent cloud
Detailed explanation of the differences between TCP and UDP
Other problems encountered in debugging fingerprints
Custom switch control
Recommended scheme of national manufactured electronic components
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Druid -- JDBC tool class case
Flink's important basics
Code007 -- determine whether the string in parentheses matches
selenium模式下切换窗口,抓取数据的实现
Mysql50 basic exercises
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
Practice and exploration of knowledge map visualization technology in meituan
洛谷P2731骑马修栅栏
Introduction to raspberry pie 3B - system installation
Unity rawimage background seamlessly connected mobile
[winui3] write an imitation Explorer file manager
Excel protects worksheets and workbooks from damage
Unity攝像頭跟隨鼠標旋轉
Getprop property