mysql> show databases; #查看数据库列表
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql; #切换到mysql库中
Database changed
mysql> show tables; #查看库里的表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
....省略
mysql> describe user; #显示数据表的结构(字段)
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
DDL语句是什么作用
DDL语句创建库,表的命令
字段定义
create table表名(字段01名称 字段01类型 字段01约束,字段O2名称 字段02类型 字段02约束,…)
字段01名称:属性名称,自定义
字段类型:
int(5) 整型 00000-99999
double 浮点型 8字节
decimal(5,2) 有效数字是5位 小数点后面保留2位 100.00 099.50
float单精度浮点 4字节
char(10) 固定长度字符串
varchar(50) 可变长度字符串
char 字符
字段01约束:
非空约束:内容不允许为空 not null
主键约束:非空且唯一标识 primary key
默认值:假如没有填数据,默认预先设定的值填写 default
自增特性: id 1234 auto increment
mysql> create database aaa; #创建名叫aaa库
Query OK, 1 row affected (0.00 sec)
mysql> use aaa #进入aaa库
Database changed
mysql> create table bbb( #创建表
-> name char(16)not null, #名称不为空,16个字符
-> passwd char(48) default '', #密码默认为空,密码48个字符
-> primary key(name)); #主键
Query OK, 0 rows affected (0.01 sec)
向数据表中插入新的数据记录命令
insert into 表名(字段1,字段2,…)values(字段1的值,字段2的值,…)
mysql> create table test (id int(4) not null primary key,name char(10) not null,address varchar(50) default '666666');
#创建名为test的表,id为整形,4个字节且不为空,主键,名字10个字符不为空,地址默认66666,50个字符
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| bbb |
| test |
+---------------+
2 rows in set (0.00 sec)
mysql> insert into test values(2,'王二','南京');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(1,'张三','上海');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(3,'李四','北京');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test; #查看test表所有内容
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | 张三 | 上海 |
| 2 | 王二 | 南京 |
| 3 | 李四 | 北京 |
+----+--------+---------+
3 rows in set (0.00 sec)
修改,更新数据表中的数据记录的命令
update 表名 set 字段名 1=值1[,字段名2=值2] where条件表达式
mysql> update test set name='麻子' where id=1 or id=2; #修改id1和id4的name
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from test;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | 麻子 | 上海 |
| 2 | 麻子 | 南京 |
| 3 | 李四 | 北京 |
+----+--------+---------+
3 rows in set (0.00 sec)
mysql> update test set name='张三'; #不加where表示修改所有id的name
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from test;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | 张三 | 上海 |
| 2 | 张三 | 南京 |
| 3 | 张三 | 北京 |
+----+--------+---------+
3 rows in set (0.00 sec)
在数据表中删除指定的数据记录命令
delete from 表名 where条件表达式
mysql> delete from test where id=3; #删除id是3的数据
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | 张三 | 上海 |
| 2 | 张三 | 南京 |
+----+--------+---------+
2 rows in set (0.00 sec)
'不带where条件的语句表示删除表中所有记录(谨慎操作)'
查看表结构命令
mysql> desc test;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(4) | NO | PRI | NULL | |
| name | char(10) | NO | | NULL | |
| address | varchar(50) | YES | | 666666 | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
不指定条件查询命令
SELECT字段名1,字段名2…FROM表名
mysql> select id,name from test;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 张三 |
+----+--------+
2 rows in set (0.00 sec)
指定条件查询的命令
SELECT字段名1,字段名2…FROM表名 WHERE条件表达式
mysql> select name,address from test where id=2;
+--------+---------+
| name | address |
+--------+---------+
| 张三 | 南京 |
+--------+---------+
1 row in set (0.00 sec)
mysql> select name,id from test where address='南京';
+--------+----+
| name | id |
+--------+----+
| 张三 | 2 |
+--------+----+
1 row in set (0.00 sec)
设置用户权限的命令
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
#all privileges:所有权限,%:所有终端
Query OK, 0 rows affected, 1 warning (0.00 sec)
查看用户权限的命令
mysql> show grants for 'root'@'%';
+-------------------------------------------------------------+
| Grants for root@% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
撤销用户权限的命令
mysql> revoke all privileges on *.* from 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'root'@'%';
+----------------------------------------------------+
| Grants for root@% |
+----------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'%' WITH GRANT OPTION |
+----------------------------------------------------+
1 row in set (0.00 sec)