mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zhangruilin |
+--------------------+
5 rows in set (0.00 sec)
3.删除数据库
mysql> drop database zhangruilin;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
表操作
1.创建表
进入到zhangruilin的数据库并且创建student表,创建表头是id int not null(创建id并且不能为空),name varchar(100) not null(创建name并且不能为空限制只能输100个字符串)和age tinyint (以tinyint格式创建年龄可以为空)
mysql> use zhangruilin;
Database changed
mysql> create table student(id int not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.01 sec)
2.创建表并且加上组件的自动增长
primary key 添加组件,组件是auto_increment 自动增长
mysql> create table info (id int primary key auto_increment not null,name varchar(50) not null,age tinyint);
Query OK, 0 rows affected (0.02 sec)
2.查看数据库里的表
进入到张瑞麟库查看有哪些表
mysql> show tables;
+-----------------------+
| Tables_in_zhangruilin |
+-----------------------+
| student |
+-----------------------+
1 row in set (0.00 sec)
不用进库里面在外面查看张瑞麟库的表内容
mysql> show tables from zhangruilin;
+-----------------------+
| Tables_in_zhangruilin |
+-----------------------+
| student |
+-----------------------+
1 row in set (0.00 sec)
3.查看表头结构
在库里面查看表头结构
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
`
在库外查看表头结构
mysql> desc zhangruilin.student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.删除表
删除表选项,再查看表时已经没有了
mysql> drop table student;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> create user 'mafeifei'@'192.168.100.129' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
在主机是129的用户连接此服务器的mysql数据库,但是并没有授权
[root@129 ~]# mysql -umafeifei -p'123456' -h 192.168.100.128
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
MySQL [(none)]>
2.删除用户
mysql> drop user 'mafeifei'@'192.168.100.129';
Query OK, 0 rows affected (0.00 sec)
查看命令show
1.查看支持的所有字符集
mysql> show character set;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
2.查看当前数据库支持的所有存储引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
3.查看表的创建命令
mysql> show create table zhangruilin.student;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into student(id,name,age) value(1,'tom',5);
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 5 |
+----+------+------+
1 row in set (0.00 sec)
2.一次插入多条记录
`mysql> insert into student(id,name,age) values(4,'kule',50),(5,'luna',20);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
| 4 | kule | 50 |
| 5 | luna | 20 |
+----+-------+------+
5 rows in set (0.00 sec)
3.查看表头的内容里面age可以为空,于是插入是可以不用不写age
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into student(id,name) values(4,'kule'),(5,'luna');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
| 4 | kule | 50 |
| 5 | luna | 20 |
| 4 | kule | NULL |
| 5 | luna | NULL |
+----+-------+------+
7 rows in set (0.00 sec)
4.可以在student里面不用加任何东西插入表内容
mysql> insert into student values(9,'kule',21),(10,'luna',33);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
| 4 | kule | 50 |
| 5 | luna | 20 |
| 4 | kule | NULL |
| 5 | luna | NULL |
| 9 | kule | 21 |
| 10 | luna | 33 |
+----+-------+------+
9 rows in set (0.00 sec)
select查看语句
查看表的所有内容
`mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
| 4 | kule | 50 |
| 5 | luna | 20 |
| 4 | kule | NULL |
| 5 | luna | NULL |
| 9 | kule | 21 |
| 10 | luna | 33 |
+----+-------+------+
9 rows in set (0.00 sec)
查看表的id和name内容
mysql> select id,name from student;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | tom |
| 3 | mafei |
| 4 | kule |
| 5 | luna |
| 4 | kule |
| 5 | luna |
| 9 | kule |
| 10 | luna |
+----+-------+
9 rows in set (0.00 sec)
将表头改个别名查看
mysql> select id as i,name as n from student;
+----+-------+
| i | n |
+----+-------+
| 1 | tom |
| 2 | tom |
| 3 | mafei |
| 4 | kule |
| 5 | luna |
| 4 | kule |
| 5 | luna |
| 9 | kule |
| 10 | luna |
+----+-------+
9 rows in set (0.00 sec)
查看大于20人的名字
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
| 4 | kule | 50 |
| 5 | luna | 20 |
| 4 | kule | NULL |
| 5 | luna | NULL |
| 9 | kule | 21 |
| 10 | luna | 33 |
+----+-------+------+
9 rows in set (0.00 sec)
mysql> select name from student where age < 20;
+-------+
| name |
+-------+
| tom |
| tom |
| mafei |
+-------+
3 rows in set (0.01 sec)
在student表里查看名字是10到20之间的记录
mysql> select name from student where age between 10 and 20;
+------+
| name |
+------+
| luna |
+------+
1 row in set (0.00 sec)
在srudent查看所有以k开头的名字
mysql> select * from student where name like 'k%';
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | kule | 50 |
| 4 | kule | NULL |
| 9 | kule | 21 |
+----+------+------+
3 rows in set (0.01 sec)
在student里查看年龄是0-9的用正则表达式查看
mysql> select * from student where age rlike '^[0-9]$';
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
+----+-------+------+
3 rows in set (0.00 sec)
在student里查看年龄是空的
mysql> select * from student where age is null;
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | kule | NULL |
| 5 | luna | NULL |
+----+------+------+
2 rows in set (0.00 sec)
在student里查看年龄是非空的
mysql> select * from student where age is not null;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 3 | mafei | 6 |
| 4 | kule | 50 |
| 5 | luna | 20 |
| 9 | kule | 21 |
| 10 | luna | 33 |
+----+-------+------+
7 rows in set (0.00 sec)
在student里查看名字是tom并且年龄是5岁的记录
mysql> select * from student where name='tom' and age=5;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
+----+------+------+
2 rows in set (0.00 sec)
在student表里查看名字是tom或者年龄是20的记录
mysql> select * from student where name='tom' or age=20;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
| 5 | luna | 20 |
+----+------+------+
3 rows in set (0.00 sec)
在student表里查看名字是tom并且年龄不等于6的记录
mysql> select * from student where name='tom' and not age=6;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 5 |
| 2 | tom | 5 |
+----+------+------+
2 rows in set (0.00 sec)
在student表里按年龄排序
mysql> select * from student order by age;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 8 | chenshuo | 10 |
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 5 | zhangshan | 26 |
| 4 | sean | 28 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+-------------+------+
10 rows in set (0.00 sec)
在student表里按年龄倒序排序
mysql> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 9 | wangwu | 100 |
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
+----+-------------+------+
10 rows in set (0.00 sec)
在student表里按年龄排序并且显示前2位
mysql> select * from student order by age limit 2;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 8 | chenshuo | 10 |
| 10 | qiuyi | 15 |
+----+----------+------+
2 rows in set (0.00 sec)
在student表里按年龄排序并且跳过前2位从第3位开始显示4个
mysql> select * from student order by age limit 2,4;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
+----+-------------+------+
4 rows in set (0.00 sec)
在student表里显示非空的并且按年龄排序
mysql> select * from student where age is not null order by age;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 8 | chenshuo | 10 |
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 5 | zhangshan | 26 |
| 4 | sean | 28 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+-------------+------+
10 rows in set (0.00 sec)
update语句
将名字是lisi的年龄修改为22
mysql> update student set age=22 where name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name='lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 22 |
+----+------+------+
1 row in set (0.00 sec)
将id=7的修改年龄是23,名字是haha。
mysql> select * from student where name='lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 22 |
+----+------+------+
1 row in set (0.00 sec)
mysql> update student set age=23,name='haha' where id=7;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where id=7;
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | haha | 23 |
+----+------+------+
1 row in set (0.00 sec)