中国网站建设市场排名,电商运营方案,无代码应用搭建平台,做网站被骗预付款怎么办一、前言
MySQL提供了大量的SQL语句用于管理。很多时候#xff0c;通过SSH远程连接时#xff0c;只能使用SQL命令#xff0c;所以#xff0c;了解并掌握常用的SQL管理操作是必须的。
二、管理MYSQL
输入SQL后#xff0c;记得加一个;#xff0c;再回车执行该语句。虽然…一、前言
MySQL提供了大量的SQL语句用于管理。很多时候通过SSH远程连接时只能使用SQL命令所以了解并掌握常用的SQL管理操作是必须的。
二、管理MYSQL
输入SQL后记得加一个;再回车执行该语句。虽然有些不需要但是MYSQL里面select不加;则会让客户端继续等待输入。如果在图形界面或则会程序开发中集成则不用加
2.1 创建数据库 create database test;
2.2 删除数据库 drop database test;
*注意如果删除数据库其所有表会被删除。
所以在删除数据库的时候要先切换为当前数据库然后在删除。 use test;
2.3 表相关操作
2.3.1 查看所有表 show tables;
2.3.2 查看表结构 desc students;
2.3.3 查看创建表 show create table students;
2.3.4 创建和删除表 create table students
drop table students
2.3.5 修改表
//修改表会更复杂一些
//给表students添加一列birth
alter table students add column birth varchar(10) not null;
//修改birth列改为birthday类型改为varchar(20)
alter tabble students change cloumn birth birthday varchar(20) not null;
//删除列birthday
alter table students drop cloumn birthday;
2.3.6 退出MYSQL exit//仅断开客户端和服务器的连接服务器仍继续运行。
三、实用的SQL语句 3.1 插入或替换
insert则如果有存在则需要删除再插入。
如果replace则不必先先查询再决定是否先删除后插入。 replace into studentsid,class_id,name,gender,scorevalues(1,1,‘XM’,‘F’,99)
//若id1先删除后再插入新记录。不然则用replace插入新记录。
3.2 插入或更新
如果记录存在则更新记录可以使用insert into …on duplicate key update…
insert into studentsid,class_id,name,gender,scorevalues(1,1,‘XM’,‘F’,99) on duplicate key update name小明‘,gender‘F’,score99;
//id1 记录被更新更新字段由update指定。否则id1不存在insert将插入新记录。
3.3 插入或忽略
语法insert ignore into
如果存在则忽略什么也不做。如果存在不存在则插入一条。
insert ignore into studentsid,class_id,name,gender,scorevalues values(1,1,‘XM’,‘F’,99);
3.4 快照
复制一份表则可以使用create table和select。创建的表和查询表结构一致。
create table classstudent1 select *from students where class_id1;
3.5 写入查询结果集
语法insert select
insert into static(classid,average) select classidavg(score) from students group by class_id;
3.6 强制使用制定索引
查询的时候回自动选择索引但不一定是最优。如果知道如何选择索引那么可以强制使用索引。
语法force index前提是使用的索引要存在。
案例select * from students force index (idxclass_id) where classid1 order by id desc;
四、参考文章
管理MYSQL 实用MYSQL语句