1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| # 创建库 mysql> create database test01; Query OK, 1 row affected (0.00 sec) # 查看库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test01 | +--------------------+ 5 rows in set (0.00 sec) # 查看当前登录的账户 mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) # 切换库 mysql> use test01 Database changed # 查看库里的表 mysql> show tables; Empty set (0.00 sec) # 查看test01数据库的编码类型 mysql> show create database test01; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | test01 | CREATE DATABASE `test01` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) # 创建表 mysql> create table students (id int unsigned not null primary key,name varchar (20) not null ,age tinyint unsigned ); Query OK, 0 rows affected (0.01 sec) # 插入数据 mysql> insert into students (id,name,age) values(1,'xiaoming',20); Query OK, 1 row affected (0.01 sec) # 改数据 mysql> update students set name='lishi',id=2; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 # 查数据 mysql> select id,name,age from students; +----+-------+------+ | id | name | age | +----+-------+------+ | 2 | lishi | 20 | +----+-------+------+ 1 row in set (0.00 sec) # 别名 select 表达式 as 别名 from 表名; mysql> select id,name,age as 年龄 from students; +----+-------+--------+ | id | name | 年龄 | +----+-------+--------+ | 2 | lishi | 20 | +----+-------+--------+ 1 row in set (0.00 sec) # 查数据 mysql> select id,name,age from students; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | xiaoming | 20 | | 2 | lishi | 20 | | 3 | xiaoming | 20 | | 4 | wangwu | 31 | +----+----------+------+ 4 rows in set (0.00 sec) # 去重 select distinct 列名 from 表名; mysql> select distinct name from students; +----------+ | name | +----------+ | xiaoming | | lishi | | wangwu | +----------+ 3 rows in set (0.00 sec) # 排序 mysql> select name,age from students order by age desc; +----------+------+ | name | age | +----------+------+ | wangwu | 31 | | xiaoming | 20 | | lishi | 20 | | xiaoming | 20 | +----------+------+ 4 rows in set (0.00 sec) # 删除 mysql> delete from students where name='xiaoming'; Query OK, 2 rows affected (0.00 sec)
|