MySQL password reset & database backup restore & foreign key constraints (multi-table relationship)

reset mysl password:

1: Stop the mysql service;
2: Start the mysqld service under cmd
mysqld --skip-grant-tables (bypass permissions to enter mysql)
3: Restart the cmd command line (you do not need to enter a password to log in to mysql)
4: Modify the root password
a:use mysql;
b:update user set password = password('abc') where user = 'root';
5: End the mysqld process
6: Restart the mysql service

Backup of the database:

cmd command line window (mysqldump -u root -p database name to be backed up > target path)

Restoring the database:

The first one: under the cmd command line window (mysql -u root -p database name to be backed up < path of the backed up file)
The second: switch to the database to be backed up, use soure backup (use mydb; source has backed up the file path)

Multi-table design foreign key constraints:

Create a department table:
create table dept(
did int primary key auto_increment,
dname varchar(20)
)
Create an employee table:
create table employee(
eid int primary key auto_increment,
ename varchar(20),
)
Inserting an employee without a department and deleting a department with an employee are not allowed. At this time, foreign key constraints need to be added between multiple tables.
 
 
Add foreign key on employee table
alter table employee add foreign key (dno) references dept(did);
Set foreign key to non-null
alter table employee modify dno int not null;

Multi-table relationship:

one-to-many
  Create a foreign key on the many side to point to the primary key on the one side.
many-to-many
  An intermediate table needs to be created. At least two fields in the intermediate table are used as foreign keys to point to the primary key of both sides of the many-to-many.
one-on-one
  Method 1: Assuming that it is one-to-many, create a foreign key on the many side to point to the primary key of the one side, and set the foreign key to unique.
  Method 2: Establish a corresponding relationship between the primary keys of the two tables.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325267622&siteId=291194637