1.创建数据库
create database Market;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Market |
| db1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
2.创建数据表
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);
Query OK, 0 rows affected (0.01 sec)mysql> desc customers;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| c_num | int(11) | NO | PRI | NULL | auto_increment |
| c_name | varchar(50) | YES | | NULL | |
| c_contact | varchar(50) | YES | | NULL | |
| c_city | varchar(50) | YES | | NULL | |
| c_birth | datetime | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.03 sec)
3.将contact插入到birth字段后面
mysql> alter table customers modify c_contact varchar(50) after c_birth;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc customers;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| c_num | int(11) | NO | PRI | NULL | |
| c_name | varchar(50) | YES | | NULL | |
| c_city | varchar(50) | YES | | NULL | |
| c_birth | datetime | NO | | NULL | |
| c_contact | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
4.将c_name字段数据类型改为VARCHAR(70)。
mysql> alter table customers modify c_name varchar(70);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc customers;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| c_num | int(11) | NO | PRI | NULL | |
| c_name | varchar(70) | YES | | NULL | |
| c_city | varchar(50) | YES | | NULL | |
| c_birth | datetime | NO | | NULL | |
| c_contact | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
5.将c_contact字段改名为c_phone。
mysql> alter table customers change c_contact c_phone varchar(50);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc customers;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| c_num | int(11) | NO | PRI | NULL | |
| c_name | varchar(70) | YES | | NULL | |
| c_city | varchar(50) | YES | | NULL | |
| c_birth | datetime | NO | | NULL | |
| c_phone | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
6.增加c gender字段,数据类型为CHAR(1)。
(7)将表名修改为customers_info。
(8)删除字段c_city.
(9)修改数据表的存储引擎为MyISAM。
(1)创建数据表orders,在o_num字段上添加主键约束和自增约束,在c_id字段上添加外键约束,关联customers表中的主键c_num。
create table orders( o_num int(11) primary key auto_increment, o_date date, o_id varchar(50) REFERENCES customers_info(c_num));
mysql> desc orders;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| o_num | int(11) | NO | PRI | NULL | auto_increment |
| o_date | date | YES | | NULL | |
| o_id | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)mysql> drop table customers_info;
Query OK, 0 rows affected (0.00 sec)
mysql> create table player( playid int primary key, playname varchar(30) not null, teamnum int not nul
l unique, info varchar(50));
Query OK, 0 rows affected (0.01 sec)mysql> show tables;
+----------------+
| Tables_in_Team |
+----------------+
| player |
+----------------+
1 row in set (0.00 sec)
(1)创建一个新账户,用户名为accountl,该用户通过本地主机连接数据库,密码为oldpwd1。授权该用户对Team数据库中 player表的SELECT和 INSERT权限,并且授权该用户对player表的info字段的UPDATE权限。
mysql> grant select,insert on Team.player to account1@'%' identified by 'oldpwd1';
Query OK, 0 rows affected, 1 warning (0.00 sec)
(2)创建SQL语句,更改account1用户的密码为newpwd2。
alter user account1@localhost identified by 'newpwd2';
(3)创建SQL语句,使用FLUSH PRIVILEGES重新加载权限表。
mysql> flush privileges;
(4)创建SQL语句,查看授权给account1 用户的权限。
show grants for account1@localhost;
(5))创建SQL语句,收回account1用户的权限。
revoke all on *.* from account1@localhost;
(6)创建SQL语句,将account1用户的账号信息从系统中删除。
drop user account1@localhost;