登录数据库
dayu@chenhedeMacBook-Pro ~ % mysql -uroot -proot123!
查看数据库所有的数据
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db_yeb |
| information_schema |
| java1 |
| mysql |
| performance_schema |
| sys |
| yeb |
+--------------------+
(注意要加分号)
选择使用yeb数据库
mysql> use yeb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
退出数据库
mysql> exit;
创建数据库(先登录mysql)
mysql> create database test;
查看创建好的数据结构
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| NAME | varchar(20) | YES | | NULL | |
| OWNER | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
查看表中的记录
mysql> select * from pet;
插入数据
mysql> INSERT INTO pet VALUES('哦哦Dl', '哈De', '章三r', 'f', '1992-09-09','2020-09-09');
删除数据
mysql> delete from pet where species='ha
更新数据库
update pet set name='a' where species='e';
查询数据库(去重)
mysql> select distinct depart from teacher;
区间查询 between and。 mysql> select * from score where degree between 60 and 80;
包含某些值查询 mysql> select * from score where degree in(85,88,86,82);
或者查询 mysql> select * from student where class='9503' or ssex='女';
升序 mysql> select * from student order by class asc; desc 降序 默认升序(asc)不用写
某个字段升序某个字段降序一起查询 mysql> select * from score order by cno asc,degree desc;
统计班级95031一共多少人 mysql> select count(*) from student where class='95031'
查询score表中的最高分的学生学号和课程号 (子查询或者排序) mysql> select sno,cno from score where degree=(select max(degree)from score);
limit 1 4。 从第一位开始查4条数据