学习《MySQL必知必会》,总结如下
1. 查看所有数据库,返回列表
show databases;
2. 创建新的数据库
create database database_name;
3. 选择数据库
use crashcourse;
4. 查看当前选择的数据库内的表,返回列表
show tables;
(新创建的数据库crashcourse,没有表,显示空)
5. 创建表,包含表名,列的细节
CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
)
CREATE TABLE customers
(
cust_id int NOT NULL AUTO_INCREMENT,
cust_name varchar(50) NOT NULL,
cust_address varchar(50) ,
cust_city varchar(50) ,
cust_state varchar(5),
cust_zip varchar(10),
cust_country varchar(50),
cust_contact varchar(50),
cust_email varchar(255),
PRIMARY KEY (cust_id)
) ENGINE=InnoDB;
再执行show tables;就显示一条记录
6.显示表列,2种方式
show columns from table_name;
每个字段返回一行,行中包含字段名、数据类型、是否允许NULL、键信息、默认值及其他信息
(如cust_id的auto_increment)
DESCRIBE语句作为SHOW COLUMNS FROM 的一种快捷方式
7.显示广泛的服务器状态信息
show status (返回内容较多)
8.显示创建特定数据库
show create database database_name;
9.显示创建特定表
show create table table_name;
10.显示授予用户(所有用户或特定用户)的安全权限
show grants;
11.显示服务器错误或警告消息
show errors;
show warnings;
12.查看所有允许的show语句
help show;
13. 向表中插入完整的一行
INSERT INTO 表名称 VALUES (值1, 值2,....)
表中包含自增列时,插入完整行,自增列(id)的值设为null或者0,MySQL会自己做处理
insert into customers
values(NULL,
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
insert into customers
values(NULL,
'Carol',
'166 Main Street',
'SuZhou',
'JS',
'215000',
'China',
NULL,
'123@126.com');
insert into customers
values(NULL,
'Jack',
'Fuzi Miao Street',
'NanJing',
'JS',
'217000',
'China',
NULL,
'12322@126.com');
insert into customers
values(NULL,
'Jacky',
'GuanQian Street',
'SuZhou',
'JS',
'217000',
'China',
NULL,
'12327@126.com');
14. 检索单个列
select column from table_name;
select cust_id from customers;
15.检索多个列
select column1,column2, .. from table_name;
select cust_id, cust_name, cust_email from customers;
16. 检索所有列,使用星号(*)通配符
select * from customers;
17. 检索唯一不同的值,使用distinct关键字
select distinct cust_city from customers;
18. 限制结果,返回某几行数据,使用limit子句
select cust_name from customers limit 2;
行0检索出来的第一行为行0,而不是行1
还可指定开始的行和行数,
select cust_name from customers limit 2,3;
select cust_name from customers limit 3 offset 2;
表示行2开始取3行(因为共4行,所以只返回了2行数据)
19. 使用完全限定的表名或列名
select customers.cust_city from crashcourse.customers;
20.对输出进行排序,使用order by 子句
select cust_name from customers order by cust_name;
也可以使用非检索的列进行排序,还可以按多列进行排序
select cust_id,cust_city,cust_name from customers order by cust_name,cust_country;
降序排序需指定DESC关键字;默认是升序排序,关键字为ASC
DESC只应用到直接位于其前面的列名,在多个列上降序排序,必须对每个列名指定DESC关键字
使用order by 和 limit 的组合,可以得到一列中最高或最低的值
21.过滤条件,使用where子句
检查单个值
select cust_id,cust_name from customers where cust_name='jack';
范围值检查用between操作符,customers表中增加一列age,并对每行数据补上age值
select cust_name from customers where age between 30 and 40;
空值检查
NULL
select cust_name from customers where cust_email is NULL;
22.AND操作符
为了通过不止一个列进行过滤,可使用AND操作符给where子句附加条件。
select cust_id,cust_name from customers where age>28 and cust_country = 'china';
23.OR操作符
指示MySQL检索匹配任一条件的行
select cust_id, cust_name from customers where age>28 or cust_country='USA';
24.IN操作符
指定条件范围,范围中的每个条件都可以匹配
select cust_id,cust_name from customers where cust_name in('Carol','John','jack');
为什么要使用in操作符?
25.NOT操作符
否定其后跟的任意条件
select cust_id, cust_name from customers where cust_name not in ('carol','jack');
MySQL支持使用NOT对IN、BETWEEN、EXISTS子句取反
26.LIKE操作符
27.百分号(%)通配符
在搜索串中,%表示任何字符出现任意次数
select cust_name from customers where cust_name like '%Jac%';
28.下划线(_)通配符
下划线的用途与%一样,但下划线只匹配单个字符而不是多个字符
select cust_name from customers where cust_name like '_arol';
网友评论