1.创建数据库
create database database_name;
2.创建表
create table table_name( 列名 类型 … );
3.查看表结构
describe table_name;
show create table table_name;
4.修改表结构
–增加列
alter table table_name add column 列名;
–删除列
alter table table_name drop column 列名;
–更改列信息
alter table table_name change column 原列名 新列名(可以是原列名,但必须存在) 类型 …; 可以修改列名
--添加默认值
alter table table_name change column 原列名 列名 类型 default 默认值;
alter table table_name modify column 原列名 类型 ....; 不可以修改列名
-
查看某一列的信息
show columns from table_name like ‘列名’;
–查看birds表中bird_disc 列的信息
eg:show columns from birds like ‘bird_disc’; -
创建表的副本
create table table_name(new) like table_name(old);
7.修改表名
rename table table_name to table_name;
8.设置自增初始值
alter table table_name auto_increment = 值;
-----------------------------------增删改查操作-----------------------------------------------
1.插入数据
insert into table_name (列名) values(对应列的值,...),(对应列的值)...;
inset into table_name set 列名=值,列名=值 ,...
inset into table_name (列名,...)select 对应列,... from table_name2
--添加ignore ,表示服务器忽略所有的错误,并插入没有报错的行(查看错误使用: show warnings;)
inset ignore into table_name (列名,...)select 对应列,... from table_name2
--对于有重复行的数据插入(使用新数据覆盖旧数据,整行覆盖)
replace into table_name (列名,...) values ();
replace into table_name (列名,...) select 对应列,... from table_name2;
插入数据优先级
--整体优先级:insert update delete 的优先级较高,select的优先级会低一点 插入数据是会锁表,InnoDB只锁行
insert 语句有三种优先级
1. low_priority :调低优先级,在 mysql做完其他事之后再插入数据,low_priority标志使得该 INSERT 语句被放到一个队列中,并等待其他正在处理和准备处理
的请求完成后,再去执行。
2. delayed :延迟插入 ,在5.5.6之后就不再提倡使用
3.high_priority :提高优先级,因为在mysql配置中–low-priority-updates 可以将写入语句变为低优先级。在这个时候可以使用high_priority 提高优先级,因为在mysql配置中–low-priority-updates
2.查询语句
---基本查询
select column from table_name;
select * from table_name;
---条件查询
--根据条件筛选
select * from table_name where 条件;
--order by 对结果进行排序
select * from table_name where 条件 order by 需要排序的列名 asc/desc(升序/降序)
--可以对多个列进行排序,先按照写的列顺序进行排序,后面列排序不影响前面列已排好的顺序
select * from table_name where 条件 order by column1,column2... asc/desc(升序/降序)
--IN运算符 查询对应条件的值
select * from table_name where column in (值,值...);
--limit num1,num2 num1:开始位置,num2:查询多少条
select * from table_name limit num1,num2;
select * from table_name where column in (值,值...) limit num1,num2;
-- AND OR 链接两个过滤条件 NULL NOT NULL 条件过滤 like '%a'(以a结尾) / 'a%'(以a开头) / '%a%'(含有a) 模糊匹配
--group by 分组查询
select * from table_name group by num2;
--多表关联查询 birds, bird_families这两张表关联查询
SELECT common_name AS 'Bird',
bird_families.scientific_name AS 'Family'
FROM birds, bird_families
WHERE birds.family_id = bird_families.family_id
AND order_id = 102
AND common_name != ''
ORDER BY common_name LIMIT 10;
3.更新语句
UPDATE table_name SET column = value, … where 条件 ;
4.删除语句
delete from table_name [WHERE condition] [ORDER BY column] [LIMIT row_count];
-
结果集合并
union 关键字
select A AS C FROM table_name union select B from table_name2;union all
select A AS C FROM table_name union all select B from table_name2;union 和union all 的区别:
UNION在进行表链接后会筛选掉重复的记录,Union All不会去除重复记录。
Union将会按照字段的顺序进行排序;UNION ALL只是将两个结果合并后就返回。
从效率来说 union all 的效率会更高 -
join
join on
select column,column1 from table_name join table_name2 on 链接条件eg:select book.book_id,name from book join appointment on book.book_id=appointment.book_id;
join using 当关联字段名一样时可以采用using
eg:select book.book_id,name from book join appointment using(book_id);
left join on(using) 左连接查询 以左表为主表
–使用join 更新数据
UPDATE birds
LEFT JOIN conservation_status USING(conservation_status_id)
JOIN bird_families USING(family_id)
SET birds.conservation_status_id = 9
WHERE bird_families.scientific_name = ‘Ardeidae’
AND conservation_status.conservation_status_id IS NULL;
3.子查询 (查询条件可以多种组合)
select * from table_name where column=(select column from table_name2);
select * from table_name join ( select column,column1,… from table_name2 where 条件 order by asc/desc(升序/降序));