SQL练习3

1、创建数据库school,字符集为utf8

SQL练习3_第1张图片

SQL练习3_第2张图片

mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| myemployees        |
| mysql              |
| performance_schema |
| school             |
| score              |
| student            |
| sys                |
| worker             |
| 工作日期表         |
+--------------------+
11 rows in set (0.00 sec)
 
mysql> exit


2、在school数据库中创建Student和Score表素材看图
创建student表

mysql> use school
Database changed
create table student (id int(10) primary key autto_increment, name varchar(20)not null, sex varchar(4),, birth year, department varchar(20)not null, addressvarchar(50) );
Query OK, 0 rows affected, 1 warning (0.01 sec)


 

mysql> desc student;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int         | NO   | PRI | NULL    | auto_increment |
| name       | varchar(20) | NO   |     | NULL    |                |
| sex        | varchar(4)  | YES  |     | NULL    |                |
| birth      | year        | YES  |     | NULL    |                |
| department | varchar(20) | NO   |     | NULL    |                |
| address    | varchar(50) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)


 

创建score表

mysql> create table score( id int(10) primary key  aut_o_increment, Stu_id int(10) not null,  C_name varchar(2
20), Grade int(10) );
Query OK, 0 rows affected, 3 warnings (0.01 sec)


 

mysql> desc score;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int         | NO   | PRI | NULL    | auto_increment |
| Stu_id | int         | NO   |     | NULL    |                |
| C_name | varchar(20) | YES  |     | NULL    |                |
| Grade  | int         | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


3、授权用户tom,密码Mysql@123,能够从任何地方登录并管理数据库school。

mysql> create user 'tom'@'%'identified by 'mysql';
mysql> grant all privileges on school to 'tom'@'%';


 
4、使用mysql客户端登录服务器,重置root密码.
[root ~]# mysqld --user=mysql --skip-grant-tables
[root my.cnf.d]# mysql – 在另一个会话中直接输入MySQL登录,此时无密码
mysql> flush privileges;刷新权限表
mysql> alter user root identified by'Yyj123456!';
 

你可能感兴趣的:(学习)