一、在数据库中创建一个表student,用于存储学生信息
CREATE TABLE student(
id INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
grade FLOAT
);
第一步:
create table student (
id int primary key,
name varchar(20) not null,
grade float
);
由于题目没有要求id字段自动递增,所以插入数值时需要特别注意id的值,但是可以在创建表时将id字段定义为AUTO_INCREMENT类型。这样每次插入新记录时,如果没有显式指定id的值,数据库会自动为它分配下一个可用的整数值。
1、向student表中添加一条新记录
记录中id字段的值为1,name字段的值为"monkey",grade字段的值为98.5
第二步:
insert into student (id, name, grade) values (1, 'monkey', 98.5);
2、向student表中添加多条新记录
2,"bob",95.5
3,"john",90.0
4,"smith",88.5
第三步:
insert into student (id, name, grade) values
(2, 'bob', 95.5),
(3, 'john', 90.0),
(4, 'smith', 88.5);
3、向student表中添加一条新记录,部分数据插入
5,"jone"
第四步:
insert into student (id, name) values (5, 'jone');
目前表中信息如图所示:
4、更新表,grade 大于90的加0.5
第五步:
update student set grade = grade + 0.5 where grade > 90;
5、删除成绩为空的记录
第六步:
delete from student where grade is null;
完成要求后,数据库中student表的数据如图所示:
二、用户权限部分
1、创建一个用户test1使他只能本地登录拥有查询student表的权限。
create user 'test1'@'localhost' identified by 'password';
grant select on student.* to 'test1'@'localhost';
2、查询用户test1的权限。
show grants for 'test1'@'localhost';
如图所示:
3、删除用户test1。
drop user 'test1'@'localhost';
查看数据库中所有的用户名和主机名
use mysql;
select User, Host FROM user;
如图所示: