[MySQL database table operation]

1. Create the table customers in the database Market

insert image description here

1. Create a database

#创建数据库
mysql> create database Market;
mysql> use Market;

2. Create a data table

#创建数据表
mysql> create table customers(
    -> c_num int(11) primary key auto_increment,
    -> c_name varchar(50),
    -> c_contact varchar(50),
    -> c_city varchar(50),
    -> c_birth datetime not null 
    -> );

insert image description here

3. Insert the c_contact field after the c_birth field

mysql> alter table customers modify column c_contact varchar(50) after c_birth;

insert image description here

4. Change the data type of the c_name field to varchar(70)

mysql> alter table customers modify c_name varchar(70);

insert image description here

5. Change the c_contact field to c_phone

mysql> alter table customers change c_contact c_phone varchar(50);

insert image description here

6. Add c_gender field, the data type is char(1)

mysql> alter table customers add c_gender char(1);

insert image description here

7. Change the table name to customers_info

mysql> rename table customers to customers_info;

insert image description here

8. Delete the field c_city

mysql> alter table customers_info drop c_city;

insert image description here

9. Modify the storage engine of the data table to MyISAM.

mysql> alter table customers_info engine=MyISAM;
#通过下列命令查看
mysql> SHOW TABLE STATUS LIKE 'customers_info';

2. Create table orders in Market

insert image description here

1. Create a data table

mysql> create table orders(
    -> o_name int(11) primary key auto_increment,
    -> o_date date,
    -> c_id int(50) 
    -> );
#因为引擎和类型不一致,所以创建不了外键,所以c_id改为int型
mysql> alter table customers_info engine=InnoDB;
#外键,将 "c_id" 列与 "customers_info" 表的主键 "c_num" 进行关联
mysql> alter table orders  add foreign key (c_id) references customers_info(c_num);

2. Delete the foreign key constraint on the orders table, and then delete the table customers_info.

#查看外键约束名称CONSTRAINT
mysql> show create table orders;
#删除外键
mysql> alter table orders drop foreign key orders_ibfk_1;
#删除表
mysql> drop table customers_info;

3. Database Team

insert image description here

1. Create a new account, the user name is account1, the user connects to the database through the local host, and the password is oldpwd1. Authorize the user to SELECT and INSERT the player table in the Team database, and authorize the user to UPDATE the info field of the player table.

mysql> create user 'account1'@'localhost' identified by 'Oldpwd1.';
mysql> grant select,insert on Team.player to account1@localhost;
mysql> grant update(info) on Team.player to account1@localhost;
#刷新权限表
mysql> FLUSH PRIVILEGES;

2. Create an SQL statement to change the password of the account1 user to newpwd2.

mysql> alter user account1@localhost identified by 'Newpwd2.';

3. Create an SQL statement and use FLUSH PRIVILEGES to reload the privilege table.

mysql> FLUSH PRIVILEGES;

4. Create an SQL statement to view the permissions authorized to the account1 user.

mysql> show grants for account1@localhost;

insert image description here

5. Create an SQL statement to withdraw the authority of the account1 user.

mysql> revoke all privileges on *.* from account1@localhost;

6. Create an SQL statement to delete the account information of the account1 user from the system.

mysql> drop user account1@localhost;

Guess you like

Origin blog.csdn.net/HealerCCX/article/details/131578348