mysql数据库基础SQL语句<一>
SQL语句
1)SQL(Structured Query Language)结构化查询语言.
SQL是一个ANSI标准计算机语言,设计用来访问、操作数据库系统。
几乎所有现今的关系型数据库软件(MySQL、MSAccess、DB2、informix、MS SQL Server、Oracle、Sybase等等)都使用SQL进行查询、管理及常用操作。
2)SQL语句可以面向数据库执行查询、从数据库取回数据、插入新的记录、更新数据、删除记录、创建新表、创建存储过程、创建视图、设置表、存储过程和视图的权限。
3)数据在数据库中以表格的形式保存。每一列是一个属性,每一行是一条记录。
4)SQL分类
DDL(Data Definition Language)数据定义语言
DML(Data Manipulation Language)数据库维护语言
DCL (Data Control Language) 数据控制语言
TCL (Transaction Control) 数据特性语言
操作语句,约定俗成用大写,以分号结尾,可以一条语句执行多个命令。也可以用小写。
1.查看数据库
mysql> show databases; mysql> SHOW DATABASES;
2.新建数据库
create database new; mysql> CREATE DATABASE NEW;
3.查看版本
mysql> select version(); mysql> SELECT VERSION();
4.查看帮助
help version; mysql> HELP VERSION;
5.查看时间
select current_time; mysql> SELECT CURRENT_TIME;
6.可做基本运算
mysql> select 4*4; mysql> SELECT 2*8;
7.删除数据库
drop database new; mysql> DROP DATABASE NEW;
8.切换当前数据库(使用数据库)
mysql> use test1; Database changed mysql> USE TEST1 Database changed
9.查看表信息
mysql> show tables; mysql> SHOW TABLES;
10.创建表格
CREATE TABLE 表名(列名1 数据类型,列名2 数据类型,列名3 数据类型)
・每一列指定一个数据类型;
mysql> create table t1 ( id int(4), name varchar(16)); 或 mysql> create table table2( -> id int(4), -> class int(4), -> grade char(4) -> ); Query OK, 0 rows affected (0.07 sec)
数据类型 |
用途 |
Integer(size) int(size) smallint(size) tinyint(size) |
存储整数数据 |
Decimal(size) numeric(size) |
存储浮点数据(小数点) |
Char(size) |
存储固定长度字符(固定长度为size) |
Varchar(size) |
存储可变长度字符(最大长度为size) |
Data(yyymmdd) |
存储日期 |
belb | |
text |
11.显示create table语句创建表的详细信息
mysql> show create table t1; +-------+--------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(4) DEFAULT NULL, `name` varchar(16) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
#或者: mysql> show create table t1\G *************************** 1. row *************************** Table: t1 Create Table: CREATE TABLE `t1` ( `id` int(4) DEFAULT NULL, `name` varchar(16) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
12.获取表结构,查看表的详细信息
1)desc t1;
mysql> desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
2)describe t1;
mysql> describe t1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
3)select * from t1;
mysql> select * from t1; Empty set (0.00 sec) mysql> desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
13.查看表索引
mysql> show index from t1;
14.检查表是否完整
mysql> check table t1; +----------+-------+----------+----------+ | Table | Op | Msg_type | Msg_text | +----------+-------+----------+----------+ | test1.t1 | check | status | OK | +----------+-------+----------+----------+ 1 row in set (0.01 sec)
13.修复损坏的表
mysql> repair table t1; +----------+--------+----------+----------+ | Table | Op | Msg_type | Msg_text | +----------+--------+----------+----------+ | test1.t1 | repair | status | OK | +----------+--------+----------+----------+ 1 row in set (0.00 sec)
备注:
Msg_type
Msg_text
Op
14.查看表的数据
mysql> select id,name from t1;
15.explain,显示mysql如何使用索引来处理select语句以及连接表。
mysql> explain select id,name from t1; +----+-------------+-------+--------+---------------+------+---------+------+------+---------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+------+---------+------+------+---------------------+ | 1 | SIMPLE | t1 | system | NULL | NULL | NULL | NULL | 0 | const row not found | +----+-------------+-------+--------+---------------+------+---------+------+------+---------------------+ 1 row in set (0.00 sec)
explain详细信息:http://database.51cto.com/art/200912/168453.htm [需总结]
mysql> explain select * from t1; +----+-------------+-------+--------+---------------+------+---------+------+------+---------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+------+---------+------+------+---------------------+ | 1 | SIMPLE | t1 | system | NULL | NULL | NULL | NULL | 0 | const row not found | +----+-------------+-------+--------+---------------+------+---------+------+------+---------------------+ 1 row in set (0.00 sec)
16.删除表
DROP TABLE table-name
mysql> drop table t1;
17.重命名表
ALTER TABLE 原名 RENAME 新表名;
mysql> alter table table1 rename students; Query OK, 0 rows affected (0.00 sec) mysql> ALTER TABLE table2 RENAME class; Query OK, 0 rows affected (0.00 sec)
18.表中添加列
ALTER TABLE 表名 ADD 列名 数据类型;
mysql> alter table cost add remark varchar(50); Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0
查看表里添加的列的结果:
mysql> show create table cost\G *************************** 1. row *************************** Table: cost Create Table: CREATE TABLE `cost` ( `id` int(4) DEFAULT NULL, `CityLevel` char(16) DEFAULT NULL, `province` char(8) DEFAULT NULL, `num` int(5) DEFAULT NULL, `price` int(5) DEFAULT NULL, `total` int(10) DEFAULT NULL, `remark` varchar(50) DEFAULT NULL #新添加的列 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
19.表中删除列
ALTER TABLE 表名 DROP COLUMN 列名;
mysql> alter table cost drop column price; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0
查看表里删除的列的结果:
mysql> show create table cost\G *************************** 1. row *************************** Table: cost Create Table: CREATE TABLE `cost` ( `id` int(4) DEFAULT NULL, `CityLevel` char(16) DEFAULT NULL, `province` char(8) DEFAULT NULL, `num` int(5) DEFAULT NULL, `total` int(10) DEFAULT NULL, `remark` varchar(50) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
20.修改表的数据类型
ALTER TABLE 表名 MODIFY 列名 数据类型;
mysql> alter table cost modify province char(10); Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0
查看表里修改的province列的数据类型结果:
mysql> show create table cost\G *************************** 1. row *************************** Table: cost Create Table: CREATE TABLE `cost` ( `id` int(4) DEFAULT NULL, `CityLevel` char(16) DEFAULT NULL, `province` char(10) DEFAULT NULL, #修改后的数据类型为char(10) `num` int(5) DEFAULT NULL, `total` int(10) DEFAULT NULL, `remark` varchar(50) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
21.重命名列
ALTER TABLE 表名 CHANGE COLUMN 列名 新列名 数据类型;
mysql> alter table cost change column num number int(5); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0
查看重命名结果:
mysql> show create table cost\G *************************** 1. row *************************** Table: cost Create Table: CREATE TABLE `cost` ( `id` int(4) DEFAULT NULL, `CityLevel` char(16) DEFAULT NULL, `province` char(10) DEFAULT NULL, `number` int(5) DEFAULT NULL, #列名num重命名为number `total` int(10) DEFAULT NULL, `remark` varchar(50) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
22.表中插入数据
INSERT INTO 表名称 VALUES (值1,值2,……); //对应的每一个属性
INSERT INTO 表名称 (列1,列2) VALUES (值1,值2);//插入指定属性
(1)插入对应的每一个属性:
mysql> insert into cost values (1,"onelevel","Beijing",32,5775,9625); Query OK, 1 row affected (0.00 sec) mysql> insert into cost values (1,'onelevel','Beijing',32,5775,9625); Query OK, 1 row affected (0.00 sec)
备注:字符串要加引号,实践得出单引号双引号都ok。
查看表中插入的数据:
mysql> select * from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 1 | onelevel | Beijing | 32 | 5775 | 9625 | +------+-----------+----------+--------+-------+--------+ 2 rows in set (0.00 sec)
(2)插入指定属性:
mysql> insert into cost (id,CityLevel,province,number,total) value (2,"OneLevel","Shanghai",1815,3025); Query OK, 1 row affected (0.00 sec)
查看表中插入的数据:
mysql> select * from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 2 | OneLevel | Shanghai | 1815 | 3025 | NULL | +------+-----------+----------+--------+-------+--------+ 3 rows in set (0.00 sec)
23.查询表中数据
SELECT 列名称1,列名称2……FROM 表名称 //查询指定数据
SELECT * FROM 表名称 //查询所有数据
(1)查询所有数据
mysql> select * from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 2 | OneLevel | Shanghai | 1815 | 3025 | NULL | +------+-----------+----------+--------+-------+--------+ 3 rows in set (0.00 sec)
(2)查询指定数据
mysql> select id,CityLevel,province,number,total,remark from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 1 | onelevel | Beijing | 32 | 5775 | 9625 | | 2 | OneLevel | Shanghai | 1815 | 3025 | NULL | +------+-----------+----------+--------+-------+--------+ 3 rows in set (0.00 sec)
24.按条件查询数据
SELECT 列名称 FROM 表名 WHERE 列 运算符 值;
mysql> select province from cost where province = "Beijing"; +----------+ | province | +----------+ | Beijing | | Beijing | +----------+ 2 rows in set (0.00 sec)
备注:
SQL Where 支持的运算符“=”、“<>”(不等于)、“>”、“<”、“>=”、“<=”、
“BETWEEN”(在某范围之内)、“LIKE”(搜索某种模式)
。仍需补充。
25.SQL删除某记录
DELETE FROM 表名称 WHERE 列 运算符 值;
DELETE * FROM 表名称;
mysql> delete from cost where number = 32; Query OK, 2 rows affected (0.00 sec) mysql> select id,CityLevel,province,number,total,remark from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 2 | OneLevel | Shanghai | 1815 | 3025 | NULL | +------+-----------+----------+--------+-------+--------+ 1 row in set (0.00 sec)
26.SQL 更新一条记录
UPDATE 表名称 SEL 列名称=新值 WHERE 列=值
此处本人感觉不太好理解,所以举例子说明::
例1:在职工表中,给WH1仓库的职工提高10%的工资。命令执行结果如图13.49所示。
UPDATE 职工 SET 工资=工资*1.10 WHERE 仓库号="WH1"
例2: 将所有学生的年龄增加1岁:
UPDATE 学生 SET 年龄=年龄+1
例3:将cost表中所有的number乘以2
mysql> update cost set number=number*2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
更新后的结果如下:
mysql> select * from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 2 | OneLevel | Shanghai | 3630 | 3025 | NULL | +------+-----------+----------+--------+-------+--------+ 1 row in set (0.00 sec)
例4:将TowTevel(二线城市)的remark改为0.
#准备环境: mysql> insert into cost (id,CityLevel,province,number,total,remark) value (1,"TowLevel","Shanxi",1144,3450,1111); Query OK, 1 row affected (0.00 sec) mysql> select * from cost; +------+-----------+----------+--------+-------+--------+ | id | CityLevel | province | number | total | remark | +------+-----------+----------+--------+-------+--------+ | 1 | TowLevel | Shanxi | 1144 | 3450 | 1111 | | 2 | OneLevel | Shanghai | 3630 | 3025 | NULL | +------+-----------+----------+--------+-------+--------+ 2 rows in set (0.00 sec) #更新语句 mysql> update cost set remark=0 where "CityLevel" = "TowLevel"; Query OK, 0 rows affected (0.00 sec) #更新后的结果: mysql> select CityLevel,remark from cost; +-----------+--------+ | CityLevel | remark | +-----------+--------+ | TowLevel | 1111 | | OneLevel | NULL | +-----------+--------+ 2 rows in set (0.00 sec)
27.返回结果删除重复项
SELECT DISTINCT 列名称 FROM 表名称
mysql> select * from daiwei; +------+--------+--------+--------+ | id | need | name | status | +------+--------+--------+--------+ | 1 | tables | hxy | Y | | 2 | pdf | Hantao | N | | 2 | pdf | Hantao | N | | 2 | pdf | Hantao | N | | 2 | pdf | Hantao | N | | 2 | pdf | Hantao | N | +------+--------+--------+--------+ 6 rows in set (0.00 sec) mysql> select distinct id from daiwei; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) mysql> select distinct name from daiwei; +--------+ | name | +--------+ | hxy | | Hantao | +--------+ 2 rows in set (0.00 sec)
28.WHERE 条件中使用逻辑组合:AND(与) & OR(或)
SELECT * FROM 表名称 WHERE 条件1 AND 条件2;
SELECT * FROM 表名称 WHERE 条件1 OR 条件2
mysql> select * from daiwei; +------+--------+---------+--------+ | id | need | name | status | +------+--------+---------+--------+ | 2 | tables | hxy | Y | | 3 | ppt | renpeng | Y | | 1 | pdf | hantao | N | +------+--------+---------+--------+ 3 rows in set (0.00 sec) mysql> select * from daiwei where need='pdf' and status='Y'; Empty set (0.01 sec) mysql> select * from daiwei where need='ppt' and status='Y'; +------+------+---------+--------+ | id | need | name | status | +------+------+---------+--------+ | 3 | ppt | renpeng | Y | +------+------+---------+--------+ 1 row in set (0.00 sec) mysql> select * from daiwei where need='ppt' or status='Y'; +------+--------+---------+--------+ | id | need | name | status | +------+--------+---------+--------+ | 2 | tables | hxy | Y | | 3 | ppt | renpeng | Y | +------+--------+---------+--------+ 2 rows in set (0.00 sec)
29.对查询结果按指定列进行排序
SELECT * FTOM 表名 ORDER BY 列名称; //顺序排序,从小到大
SELECT * FROM 表名称 ORDER BY 列名称 DESC;
mysql> select * from daiwei; +------+--------+---------+--------+ | id | need | name | status | +------+--------+---------+--------+ | 2 | tables | hxy | Y | | 3 | ppt | renpeng | Y | | 1 | pdf | hantao | N | +------+--------+---------+--------+ 3 rows in set (0.00 sec) mysql> select * from daiwei order by id; +------+--------+---------+--------+ | id | need | name | status | +------+--------+---------+--------+ | 1 | pdf | hantao | N | | 2 | tables | hxy | Y | | 3 | ppt | renpeng | Y | +------+--------+---------+--------+ 3 rows in set (0.00 sec) mysql> select * from daiwei order by id desc; +------+--------+---------+--------+ | id | need | name | status | +------+--------+---------+--------+ | 3 | ppt | renpeng | Y | | 2 | tables | hxy | Y | | 1 | pdf | hantao | N | +------+--------+---------+--------+ 3 rows in set (0.00 sec)
30.MySQL用户
MySQL数据库默认只有一个root用户
MySQL将用户信息保存在mysql数据库user表中;
1)查看当前用户信息
mysql> select user,host from mysql.user; +---------+-----------+ | user | host | +---------+-----------+ | newuser | % | | root | 127.0.0.1 | | | localhost | | root | localhost | | | mysql | | root | mysql | +---------+-----------+ 6 rows in set (0.00 sec)
mysql> select user,host,password from mysql.user; +---------+-----------+-------------------------------------------+ | user | host | password | +---------+-----------+-------------------------------------------+ | root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | mysql | | | root | 127.0.0.1 | | | | localhost | | | | mysql | | | newuser | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | +---------+-----------+-------------------------------------------+ 6 rows in set (0.00 sec)
2)查看mysql数据库中所有用户(不推荐)
mysql> select * from mysql.user;
推荐:
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; +---------------------------+ | query | +---------------------------+ | User: 'newuser'@'%'; | | User: 'root'@'127.0.0.1'; | | User: ''@'localhost'; | | User: 'root'@'localhost'; | | User: ''@'mysql'; | | User: 'root'@'mysql'; | +---------------------------+ 6 rows in set (0.00 sec)
3)创建一个新的用户
CREATE USER 用户名 IDENTIFIED BY ‘密码’;(新用户创建后不能登录,因为没有设置权限)
mysql> create user newuser identified by '123'; Query OK, 0 rows affected (0.03 sec)
4)删除用户
DROP USER 用户名;
mysql> drop user newuser; Query OK, 0 rows affected (0.00 sec)
5)重命名用户名
RENAME USER 源用户名 TO 新用户名;
mysql> rename user test to testnew; Query OK, 0 rows affected (0.00 sec)
6)修改用户密码
修改当前用户密码 SET PASSWORD=PASSWORD(‘新密码’);
修改指定用户密码 SET PASSWORD FOR 用户名=PASSWORD(‘新密码’)
mysql> set password=password('456'); Query OK, 0 rows affected (0.00 sec)
mysql> set password for testnew=password('123'); Query OK, 0 rows affected (0.00 sec)
31.MySQL权限系统
MySQL全线系统控制一个用户是否能够进行连接,以及连接后能够针对那些对象进行什么操作。
MySQL权限控制包括两个阶段;
1)检查用户是否能够连接;
2)检查用户是否具有执行动作的权限
MySQL授予权限可以分为以下几个层级:
① 全局层级
② 数据库层级
③ 表层级
④ 列层级
⑤ 子程序层级
MySQL通过GRANT授予权限,REVIKE撤销权限。
授予一个用户权限:
GRANT ALL PRIVILEGES ON 层级 to 用户名@主机 IDENTIFIED BY’密码’
授予权限 全部权限允许用户访问的源用户的密码
a. 考虑层级
b. 考虑用户及密码
例1:GRANT ALL PRIVIEGES ON *.* to ‘nash_su’@’%’ IDENTIFIED BY ‘linuxcast’;
授予nash_su用户全局级全部权限
例2: GRANT ALL PRIVILEGES ON Linuxcast.* to ‘nash_su’@’%’ IDENTIFIED BY ‘linuxcast’授予nash_su用户针对Linuxcast数据库全部权限
mysql> grant all privileges on *.* to 'testnew'@'192.168.107.%' identified by '123'; Query OK, 0 rows affected (0.00 sec)
查看数据库中具体某个用户的权限
mysql> show grants for 'testnew'@'192.168.107.%'; +-----------------------------------------------------------------------------------------------------------------------------+ | Grants for [email protected].% | +-----------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'testnew'@'192.168.107.%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' | +-----------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> show grants for 'testnew'@'192.168.107.%'\G *************************** 1. row *************************** Grants for [email protected].%: GRANT ALL PRIVILEGES ON *.* TO 'testnew'@'192.168.107.%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' 1 row in set (0.00 sec)
32.MySQL连接认证
当连接MySQL服务时,MySQL通过用户名密码和主机信息验证是否允许连接
GRANT ALL PRIVILEGES ON *.* to ‘nash_su’@‘%’IDENTIFIED BY ‘Linuxcast’;
这里的主机是指允许从那些主机进行连接,可以使用如下形式。
所有主机‘%’
精确的主机名或ip地址 www.linuxcast.net或192.168.1.1
使用’*’通配符 linuxcast.net //所有以Linux。Net结尾的主机名
指定一个网段192.168.1.0/255.255.255.0 //此网段的主机可以连接
通常单独创建一个普通用户远程登录数据库;
Root一般没有远程登录数据库的命令,只能从本地连接;
修改root的默认远程登录的权限(但是比较危险):
GRANT ALL PRIVILEGES ON *.* to‘root’@‘%’IDENITFIED BY ‘linuxcast’;
mysql> grant all privileges on *.* to 'testnew'@'192.168.107.%' identified by '123'; Query OK, 0 rows affected (0.00 sec)