Experiment 1 database, table creation and maintenance

This article records database and table creation and maintenance related operations, including client interface operation methods and command operation methods

Experimental environment: MySQL 5.7, DataGrip

Table of contents

This article records database and table creation and maintenance related operations, including client interface operation methods and command operation methods

Experimental environment: MySQL 5.7, DataGrip

1. Purpose of the experiment

2. Experiment content (please complete the following operations)

1. Create a relational database S_T###,

2. In the S_T database, create the student table students

3. In the S_T database, create the teacher table Teachers

4. In the S_T database, create the curriculum table Courses

5. In the S_T database, create a course selection table STC

6. Modify the Students table with a statement,

7. Change the score in the STC table to smallint type by modifying the statement;

8. Set the Cno attribute in the Course table as the main code.

9. Add referential integrity constraints for cno in the STC table, and refer to the Cno attribute in the Courses table.

10. Enter the data in the following table into the four tables that have been created.


1. Purpose of the experiment

1. Master the creation and maintenance of database

2. Familiar with the creation and maintenance of data tables

3. Familiar with data update operations.

2. Experiment content (please complete the following operations)

1. Create a relational database S_T###,

The database character set is required to be utf8, and the character set collation rules are default.

Note: ### in the database name represents the last three digits of my student number. For example, Zhang Hua's student number is 20160714121, and his database name is "S_T121"

Specific operation:

Right-click MySQL and select New-Schema

 

 

2. In the S_T database, create the student table students

The storage engine of this table is InnoDB, using the default character set of the database. This table mainly includes four attributes: student number (sno), name (sname), date of birth (sbirth) and grade (grade). The data type, length and constraint requirements of each attribute are as follows:

Sno: char(10), main code;

Sname: char(4), not allowed to be empty

sbirth:date,

Grade:smallint。

 Specific operation:

Right-click the database, select New - Table

The first step, custom table name

The second step, add a new column

The third step, custom column

 

final display

 

 The syntax in the red box means that the storage engine of the table is InnoDB, and the default character set of the database is used. It’s okay not to write, because the default is the InnoDB storage engine

Note: the primary key cannot be empty

 Syntax used:

CREATE TABLE `students` (
  `Sno` char(10) NOT NULL,
  `Sname` char(4) NOT NULL,
  `sbirth` date DEFAULT NULL,
  `Grade` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`Sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

3. In the S_T database, create the teacher table Teachers

It mainly includes four attributes: teacher number (Tno), teacher name (Tname), gender (sex) and salary (salary). The data type, length and constraint requirements of each attribute are as follows:

Tno:CHAR(8) , main code;

Tname: char(4) not-null constraint,

sex: compound type, the value is 'male' or 'female',

Salary:float。

 Concrete operation

Same as creating the students table

Note that the definition of sex compound type

 syntax used

CREATE TABLE `teachers` (
  `Tno` char(8) NOT NULL,
  `Tname` char(4) NOT NULL,
  `sex` enum('男','女') DEFAULT NULL,
  `Salary` float DEFAULT NULL,
  PRIMARY KEY (`Tno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4. In the S_T database, create the curriculum table Courses

It mainly includes three attributes: class number (Cno), class name (Cname) and class hour (Chour). The data type, length and constraint requirements of each attribute are as follows:

Cno: char(8) not-null constraint,

cname: varchar(10) not-null constraint,

Chour:tinyint。

 Concrete operation

 syntax used

create table Courses
(
    Cno   char(8)     not null,
    cname varchar(10) not null,
    Chour tinyint     null
);
type size range (signed) range (unsigned) use
TINYINT 1 Bytes (-128,127) (0,255) small integer value

 

type size use
CHAR 0-255 bytes fixed length string
VARCHAR 0-65535 bytes variable length string

5. In the S_T database, create a course selection table STC

It includes four attributes: Student ID (Sno), Teacher ID (Tno), Class ID (Cno) and Grade (score). The data type, length and constraint requirements of each attribute are as follows:

Sno:char(10),

Cno:CHAR(8),

Tno:CHAR(8),

Score:int。

Set (Sno, cno) as the main code, and the Sno attribute in the STC table is the external code, refer to the student number sno in the students table.

Concrete operation

 When setting two attributes to combine into a primary key, you need to add attributes in the operation of the key

When setting a foreign key: enter the foreign key operation

1. Select the target table

2. Add foreign key column

 

6. Modify the Students table with a statement,

Add the gender Ssex attribute after the sname attribute, which is an enum type and can only be "male" or "female";

Concrete operation

Right-click the students table and enter the query console

ALTER TABLE students 
    ADD Ssex enum('男','女') AFTER Sname;

Successfully displayed after execution

 

7. Change the score in the STC table to smallint type by modifying the statement;

see operation

ALTER TABLE stc
    MODIFY Score smallint;

When modifying existing properties, use the MODIFY keyword


8. Set the Cno attribute in the Course table as the main code.

see operation

ALTER TABLE courses
    ADD PRIMARY KEY (Cno);

To add a primary key to an existing attribute, use the ADD keyword


9. Add referential integrity constraints for cno in the STC table, and refer to the Cno attribute in the Courses table.

 see operation

alter table stc
    add foreign key (Cno)references courses(Cno);  

Grammar explanation: modify the table stc, add a primary key Cno, refer to the Cno in the courses table


10. Enter the data in the following table into the four tables that have been created.

Students table:

  sno

takes off

Ssex

breath

grade

201615121

Li Li

female

1999-1-1

2016

201615122

Wang Yang

male

1998-12-21

2016

201615123

Liu Chen

female

1998-6-5

2016

201715121

Wang Xiaochen

male

1999-4-16

2017

201715122

Zhang Fang

male

1997-6-1

2017

201715123

Qiao Yunping

female

1999-8-12

2017 


Teachers table:

Tno

Tname

Sex

salary

101

Li Dawei

male

6200

102

Liu Yun

female

4500

103

Wang Jun

male

3600

104

Zhang Hongxia

female

4100

Course ScheduleCourses

Cno

Cname

Choir

B001

advanced mathematics

80

B002

Introduction to Computer

48

B003

C programming

64

B004

data structure

72

B005

Database systems

56

B006

fuzzy mathematics

56

Class ScheduleSTC

Sno

Cno

Tno

score

201615121

B001

101

78

201615121

B002

102

85

201615121

B003

103

69

201615121

B004

104

201615122

B002

102

98

201615122

B003

103

89

201615123

B001

101

65

201615123

B003

103

56

201615123

B004

104

201715121

B002

102

68

201715121

B004

104

78

201715123

B001

101

81

201715123

B002

102

60

Concrete operation

The method I choose is to copy the data to an Excel table

Plus sign, add row, select the first cell

Then copy the data in the Excel table and paste it into the first cell

 

 

Guess you like

Origin blog.csdn.net/qq_25887493/article/details/123936779