SQL语言(Structured Query Language),结构化语言,是关系型数据库的标准语言,用于维护管理数据库。
总共可分为以下四种。
名称 | 含义 |
---|---|
DDL | 数据定义语言 |
DML | 数据操纵语言 |
DQL | 数据查询语言 |
DCL | 数据控制语言 |
下面将结合LInux命令和Navicat图形界面为大家展示数据库系统的各种简单管理操作:
在安装完MySQL,设置密码之后,先进入数据库,如图,出现“mysql>”,就是在数据库内了。
mysql> use mysql; //这条命令表明使用的是哪个数据库,使用mysql数据库
show tables; //查看此数据库下的所有表
GRANT命令用于授权,如果想用连接Navicat,需要进行一个用户的授权
GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY ‘密码’]
mysql> GRANT all ON *.* TO 'admin'@'%' IDENTIFIED BY 'admin123';
//为了方便操作,将所有库的所有表的所有权限给外来地址以admin用户登录,admin123为密码
打开Navicat ,按图显示填入,先连接测试看是否成功,成功则确定。
连进去后,能看到授权的库,这里都能看到。
CREATE DATABASE 数据库名
use 数据库名
CREATE TABLE 表名 (字段定义…));
mysql> CREATE DATABASE ku; //创建数据库ku Query OK, 1 row affected (0.00 sec)
mysql> use ku; //使用ku数据库
Database changed
mysql> CREATE TABLE users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name));
//创建表users,第一列名 是user_name,字符类型为char,长度为16.不能为空,第二列名为user_passwd,字符类型为char,长度为48,默认配置,键值为user_name列。
Query OK, 0 rows affected (0.02 sec)
更直观的可以从Navicat 设计表页面看,如下图:
在linux系统中也可以用describe(desc)查看表格格式:
DROP TABLE 表名
DROP DATABASE 数据库名
mysql> use auth //先进入auth数据库
Database changed
mysql> show tables; //看一下里面的表,是空的
Empty set (0.00 sec)
mysql> CREATE TABLE users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name));
//手动创建表
Query OK, 0 rows affected (0.00 sec)
mysql> show tables; //看一下,新建的表出现了
+----------------+
| Tables_in_auth |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
mysql> DROP TABLE auth.users; //使用DROP命令删除表users
Query OK, 0 rows affected (0.00 sec)
mysql> show tables; //再看一下,是空的,删掉了
Empty set (0.00 sec)
mysql> DROP DATABASE auth; //删除数据库auth
Query OK, 0 rows affected (0.00 sec)
mysql> show databases; //查看数据库列表,没有auth了
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| ku |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
用于对表中的数据进行管理
INSERT INTO 表名(字段1,字段2,…) VALUES(字段1的值,字段2的值,…)
mysql> use ku //使用数据库ku
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> INSERT INTO users(user_name,user_passwd) values('zhangsan', password('123456'));
Query OK, 1 row affected, 1 warning (0.01 sec)
//在表中插入姓名为zhangsan ,密码为密文格式的123456的数据
mysql> INSERT INTO users values('lisi', password('abcdef'));
Query OK, 1 row affected, 1 warning (0.00 sec)
//当插入数据与表列元素一一对应的时候,可以省略前面的列名
mysql>
UPDATE 表名 SET 字段名1=值1 [,字段名2=值2] WHERE 条件表达式
mysql> UPDATE ku.users SET user_passwd=password('123456') where user_name='lisi';
//更新users表,寻找user_name为lisi的行,把user_passwd改为和zhangsan一样的密文123456
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
DELETE FROM 表名 WHERE 条件表达式
mysql> select * from users; //查看表users的所有记录
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> DELETE FROM ku.users WHERE user_name='lisi'; //删除user_name为lisi的行
Query OK, 1 row affected (0.01 sec)
mysql> select * from users; //再看一下,删掉了
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
SELECT 字段名1,字段名2… FROM 表名 WHERE 条件表达式
mysql> SELECT * FROM users; //查看表users的所有记录,*通配符表示所有
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT user_name FROM ku.users WHERE user_name='zhangsan'; //查看users表中,名字叫zhangsan 行的 姓名那一列的数据
+-----------+
| user_name |
+-----------+
| zhangsan |
+-----------+
1 row in set (0.00 sec)
在没有这个用户的时候,它会自动创建用户
GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY ‘密码’]
mysql> GRANT select ON ku.* TO 'wang'@'localhost' IDENTIFIED BY '123456';
//给本地密码为123456的wang用户数据库ku所有表的select权限,并且自动创建了wang这个用户
Query OK, 0 rows affected, 1 warning (0.00 sec)
验证
mysql> exit //先退出
Bye
[root@localhost ~]# mysql -u wang -p //以wang登录
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from ku.users; //试验select表记录 成功查看
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> drop table ku.users; //试验drop 没有权限,报错
ERROR 1142 (42000): DROP command denied to user 'wang'@'localhost' for table 'users'
SHOW GRANTS FOR 用户名@来源地址;
mysql> SHOW GRANTS FOR 'wang'@'localhost'; //查看刚刚设置的wang的权限
+----------------------------------------------+
| Grants for wang@localhost |
+----------------------------------------------+
| GRANT USAGE ON *.* TO 'wang'@'localhost' |
| GRANT SELECT ON "ku".* TO 'wang'@'localhost' |
+----------------------------------------------+
2 rows in set (0.00 sec)