Mysql常用语句
增
insert into 表 values(值,....);
删
delete 表 where 条件;
改
update 表 set 新值 where 旧值;
查
select 字段 from 表;
创建数据库
create database 数据库名;
使用数据库
use 数据库名;
删除数据库
drop database 数据库名;
查找所有数据库
show databases;
查看创建库的详细信息
show create database 数据库名;
创建表
create table 表名( 字段名 属性 auto_increment(自增长), 字段名 属性, 主键(需加主键的字段));
删除表
drop table 表名;
查找当前数据库的所有表
show tables;
查看当前表的字段
desc 表名;
查看创建表的详细信息
show create table 表名;
修改表
(1)新增字段
alter table 表名 add 字段 类型 【after 字段】;
(2)修改字段类型
alter table 表名 modify 字段 新类型;
(3)删除字段
alter table 表名 drop 字段名;
(4)修改已有字段
alter table 表名 change 原字段名 新字段名 新类型;
多表查询
(1)内连接(inner join)
select 字段 from 表A inner join 表B on 表A.ID=表B.ID;
(2)外连接(left join and right join)
①left join
select 字段 from 表A left join 表B where 表A.ID=表B.ID;
②right join
select 字段 from 表A left join 表B where 表A.ID=表B.ID;
(3)全外连接(full outer join)
select 字段 from 表A full outer join 表B where 表A.ID=表B.ID;
创建索引
create INDEX 索引名 ON 表名【列名(长度)】;直接创建 alter table 表名 add INDEX 索引名【列名(长度)】;实用alter语句创建 create table xx(id 字段类型 not null,INDEX [索引名] (列名(长度)));建表时创建
排序
order by desc|asc;
常用属性
主键 primary key 默认值属性 default 不允许为空 not null 自动增长 auto_increment 唯一键 unique key
修改字段名称
alter table 表名 change 字段名 新字段名 新字段类型 新约束条件;
创建外键(foreign key)
alter table 表名 add constraint 约束名 foreign key(外键字段名) references 外表表名(外表字段名);
创建存储过程(in为输入out输出inout可输入输出)
create procedure 过程名(in/out/inout 参数名 参数类型)
begin
select * from 表名 where id=参数名;
end
表注释COMMENT
建表时创建表注释
CREATE TABLE t1
(
id int COMMENT 'id',
name varchar(20) comment '姓名',
age int comment '年龄'
);
查看表注释
SHOW FULL COLUMNS FROM t1;
添加全文索引(create type name on 表(xx,xx,.....))
create fulltext index index_ext on workinfo(extra);
删除索引
DROP INDEX index_name ON workinfo;
已创建表添加注释
ALTER TABLE t1 CHANGE COLUMN id id INT DEFAULT 0 COMMENT '主键(修改)id';
备份
mysqldump -uroot -proot alarm > D:\alarm.sql
还原
mysql -uroot -proot alarm < D:/alarm.sql
编码问题
mysql -uroot -p --default-character-set=utf8mb4 alarm < D:/alarm.sql
修改密码
mysqladmin -uroot -p123456 password 123
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
使用存储过程
call 存储过程名称(值);
浙公网安备 33010602011771号