Data table creation and modification management (table integrity constraints)

1. Purpose of the experiment

1. Master the basic knowledge of watches.

2. Master the method of using Navicat or other third-party management tools and SQL statements to create tables.

3. Master the basic operation methods such as modifying, viewing, and deleting tables.

4. Master the definition of integrity constraints in tables.

5. Master the role of integrity constraints

2. Experimental content and requirements

1. Table definition and modification operations

Create a teacherInfo table in the schoolInfo database, the table structure is as follows:

2. Create the staffinfo database, and define the department table and worker table to complete the integrity constraints between the two tables.

Structure of the Department table

field name

field description

type of data

primary key

foreign key

non empty

only

self-increment

d_id

department number

INT(4)

yes

no

yes

yes

no

d_name

department name

VARCHAR(20)

no

no

yes

yes

no

function

Departmental functions

VARCHAR(50)

no

no

no

no

no

address

department location

VARCHAR(20)

no

no

no

no

no

The structure of the Worker table

field name

field description

type of data

primary key

foreign key

non empty

only

self-increment

id

serial number

INT(4)

yes

no

yes

yes

yes

num

employee ID

INT(10)

no

no

yes

yes

no

d_id

department number

INT(4)

no

yes

no

no

no

name

Name

VARCHAR(20)

no

no

yes

no

no

sex

gender

VARCHAR(4)

no

no

yes

no

no

birthday

date of birth

DATE

no

no

no

no

no

address

Home address

VARCHAR(50)

no

no

no

no

no

3. Experimental methods and steps

1. Table definition and modification operations

(1) First create the database schoolInfo.

(2) Create teacherInfo table.

(3) Change the data type of the name field of the teacherInfo table to VARCHAR(30).

(4) Change the position of the birthday field to the front of the sex field.

(5) Rename the num field to t_id.

(6) Delete the address field of the teacherInfo table.

(7) Add a field named wages in the teacherInfo table, and the data type is FLOAT.

(8) Rename the teacherInfo table to teacherInfo_Info.

(9) Change the storage engine of the teacherInfo_ Info table to MyISAM type.

2. Create the staffinfo database, and define the department table and worker table to complete the integrity constraints between the two tables.

(1) Create a department table and a worker table under the staffinfo database.

(2) Delete the department table.

(3) Delete the department table

(4) Delete the foreign key constraint of the worker table

(5) Delete the department table again

4. Experimental results

1. Table definition and modification operations

(1) First create the database schoolInfo.

code:

create database schoolInfo;

result:

(2) Create teacherInfo table.

code:

 create table teacherInfo (

id int(4) not null unique primary key auto_increment,

num int(10)not null,

name varchar(20) not null,

sex VARCHAR(4) not null,

birthday datetime,

address varchar(50)

);

result:

(3) Change the data type of the name field of the teacherInfo table to VARCHAR(30).

code:

alter table teacherInfo modify name varchar(30) not null;

result:

(4) Change the position of the birthday field to the front of the sex field.

code:

 alter table teacherInfo modify birthday datetime after name;

result:

(5) Rename the num field to t_id.

code:

 alter table teacherInfo change num t_id int(10) not null;

result:

(6) Delete the address field of the teacherInfo table.

code:

 alter table teacherInfo drop address;

result:

(7) Add a field named wages in the teacherInfo table, and the data type is FLOAT.

code:

alter table teacherInfo add wages float;

result:

 

(8) Rename the teacherInfo table to teacherInfo_Info.

code:

 alter table  teacherInfo rename teacherInfo_Info;

result:

(9) Change the storage engine of the teacherInfo_ Info table to MyISAM type.

code:

 alter table teacherInfo_Info engine = MyISAM;

result:

2. Create the staffinfo database, and define the department table and worker table to complete the integrity constraints between the two tables.

(1) Create a department table and a worker table under the staffinfo database.

code:

CREATE TABLE department(

d_id INT(4) NOT NULL UNIQUE PRIMARY KEY,

d_name VARCHAR(20) NOT NULL UNIQUE,

`function` VARCHAR(50),

 address VARCHAR(50)

 );

create table worker(

id int(4) not null unique primary key auto_increment,

num int(10) not null unique,

d_id int(4),

name varchar(20) not null,

sex varchar(4) not null,

birthday date,

address varchar(50)

);

result:

(2) Delete the department table.

code:

drop table department;

result:

(3) Delete the department table

code:

drop table department;

result:

Because the department table has been deleted before, the deletion fails again, and the unknown table is displayed

(4) Delete the foreign key constraint of the worker table

code:

Since the foreign key cannot be added directly when creating the table, here you need to add the foreign key after creating the table:

alter table worker add constraint d_id foreign key(d_id) references department (d_id);

worker is the name of the slave table

d_id is 1 foreign key constraint name

department is the main table name

alter table worker drop foreign key d_id;

result:

(5) Delete the department table again

code:

drop table department;

result:

5. Summary of Knowledge Points

Table integrity constraints include:

primary key #Identifies the field as the primary key of the table, which can uniquely identify the record

foreign key #Identifies the field as the foreign key of the table

not null # indicates that the field cannot be empty and must be assigned a value

unique key # identifies that the field is unique

auto_increment #Identifies the automatic growth of the value of the field (integer type, and primary key)

default # Set a default value for this field, and use the default value without assigning a value to the field when inserting a record

unsigned # unsigned (sign, etc.)

zerofill # fill with 0

 

Guess you like

Origin blog.csdn.net/m0_61607990/article/details/127160386