题目一
(1)
mysql> create database Market;
(2)
mysql> use Market;
Database changed
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.52 sec)
(3)
mysql> alter table customers modify c_contact varchar(50) after c_birth;
(4)
mysql> alter table customers modify c_name varchar(70);
(5)
mysql> alter table customers change c_contact c_phone;
(6)
mysql> alter table customers add c_gender char(1);
(7)
mysql> alter table customers rename customers_info;
(8)
mysql> alter table customers_info drop c_city;
(9)
mysql> alter table customers_info engine=MyISAM;
--
/*注: 下一题数据表要外键约束到这一题的数据表中,
由于MyISAM不支持外键约束,所以为了能完成下一题的要求,
需要将存储引擎改回为默认的InnoDB */
mysql> alter table customers_info engine=InnoDB;
题目二
(1)
create table orders(
0_num int(11) primary key auto_increment,
0_date date,
c_id int(11),
foreign key(c_id) references customers_info(c_num)
);
(2)
--目的是查看外键约束的名,进而才能删除外键
mysql> show create table orders;
--根据查看到的orders_ibfk_1,来进行删除
mysql> alter table orders drop foreign key orders_ibfk_1;
--删除表
mysql> drop table orders;
题目三
mysql> create database Team;
mysql> use Team;
Database changed
mysql> create table player(
-> playid int primary key,
-> playname varchar(30) not null,
-> teamnum int not null unique,
-> info varchar(50)
-> );
(1)
--创建账户
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;
(2)
mysql> alter user account1@localhost identified by 'newpwd2';
(3)
--刷新权限表
mysql> flush privileges;
(4)
mysql> show grants for account1@localhost;
(5)
mysql> revoke SELECT, INSERT, UPDATE (info) ON `Team`.`player` from 'account1'@'localhost';
(6)
mysql> drop user account1@localhost;
题目四
创建表:
创建员工表employee,字段如下:
id(员工编号),name(员工名字),gender(员工性别),salary(员工薪资)
mysql> create table employee(
-> id int primary key auto_increment comment '员工编号',
-> name varchar(20) not null comment '员工名字',
-> gender char(1) comment '员工性别',
-> salary int default 0 comment '员工薪资'
-> );
插入数据
1,‘张三’,‘男’,2000
2,‘李四’,‘男’,1000
3,‘王五’,‘女’,4000
注:由于我设置的id为主键且自增,所以故意不给值也会自动生成,所以只给了后面三个字段插入值
mysql> insert into employee(name,gender,salary) values
-> ('张三','男',2000),
-> ('李四','男',1000),
-> ('王五','女',4000)
-> ;
修改表数据
1 将所有员工薪水修改为5000元
2 将姓名为张三的员工薪水修改为3000元
3 将姓名为李四的员工薪水修改为4000元,gener改为女
4 将王五的薪水在原有基础上增加1000元
mysql> update employee set salary=5000;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> update employee set salary=3000 where name='张三';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employee set salary=4000 where name='李四';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employee set salary=salary+1000 where name='王五';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0