- 启动mysql服务器:net start mysql
- 停止mysql服务器:net stop mysql;
- 连接mysql服务器:mysql -u root -h127.0.0.1 -p
- 断开mysql服务器:quit、exit、\q
- 创建数据库:create database student; 或 create schema student;
- 查看数据库:show databases;
- 切换(使用,要操作的数据库):use 数据库名;
- 删除数据库:drop database student;
- 创建表:create table 表名(列名 类型,列名 类型,列名 类型);
- 查询数据库下所有的表:show tables;
- 查询表的结构:desc 表名; show columns from 表名 from 数据库名; show columns from数据库名.表名
- 创建指定字符集的数据库:create database 数据库名 character set=gbk;
-
创建数据库前判断数据库名是否存在:create database if not exists 数据库名;
-
筛选‘db_’开头的数据库:show databases like ‘db_%’;
-
添加字段:alter table 表名 add(字段 类型,字段 类型);
-
添加字段:alter table 表名 add(字段 类型,字段 类型);
-
修改字段类型:alter table 表名 modify 字段 类型;
-
修改字段名:alter table 表名 change 旧字段名 新字段名 新字段类型;
-
修改表名:alter table 旧表名 rename as 新表名;
-
删除数据表:drop table 表名;
-
向表中插入数据:insert into 表名 values(1001,’张三’,25,’男’);
insert into 表名 set id=1001,xm=’张三’,age=25,sex=’男’;
- 修改表中记录:update 表名 set age=24 where id=1001;
- 删除表中特定数据:delete from 表名 where id=1001;
- 删除表中所有数据:truncate table 表名;
- 数据库的备份:mysqldump -uroot -p123 数据库名>c:\123.sql
-
数据库的恢复:mysql -uroot -p123 数据库名<c:\123.sql
- 查询所有字段:select * from 表名;
- 查询指定字段:select 字段名(列名) from 表名;
- 查询指定数据:select * from 表名 where xm=’张三’;
- 在集合内查询(关键字in):select * from 表名 where id in(1001,1003,1005);
- 在范围内查询(between...and...):select * from 表名 where age between 20 and 30;
- 空值查询:select * from stu where li_lun is null;
- 带关键字and多条件查询:select * from stu where li_lun>90 and ji_neng>90;
- 带关键字or多条件查询:select * from stu where id=1001 or li_lun>90;
- 去重查询:select distinct xm from stu;
- 列运算:数值计算:select li_lun+ji_neng from stu;字符串连接:select concat(li_lun,’分’) from stu; 转换null值:select ifnull(li_lun,0)+ji_neng from stu;给列起别名:select ifnull(li_lun,0)+ji_neng as ‘总分’ from stu;(as可省略)
-
主键(primary key):修改表时指定主键:alter table stu add primary key(id);删除主键:alter table stu drop primary key;
- 主键自增长(primary key auto_increment):(此时主键必须设置为int) 修改表时设置主键自增长:alter table stu change sid sid int auto_increment; 修改表时删除主键自增长:alter table stu change sid sid int;
- 非空(not null):添加非空约束:alter table 表名 modify 列名 列类型 not null; 修改非空约束:alter table 表名 modify 列名 列类型 null; 删除非空约束:alter table 表名modify 列名 列类型;
- 唯一(unique): 添加唯一约束: alter table 列名 add unique(列名称) ; 修改唯一约束:alter table 表名 modify 列名称 列类型; 删除唯一约束:alter table 表名称 drop index 列名称;
- 默认值(default): 添加默认值约束:alter table 表名 modify 列名 列类型 not null default '默认值';
- 外键(foreign key):constrain fk_id foreign key(sid) references sheng_fen(id) 添加外键约束:alter table student add constrain fk_id foreign key(sid) references sheng_fen(id); 删除外键约束:alter table student drop foreign key fk_id;
- 升序:select * from student order by age asc;
- 倒序:select * from student order by age desc;
- 模糊查询:_ 表示单个的任意字符 % 表示任意长度的任意字符
例如:查询姓张的所有同学的记录:select * from student where sname like ‘张%’; 查询姓名中第二个字为‘亮’的记录:select * from student where sname like ‘_亮%’ 查询姓名中包含‘明’字的记录:select * from student where sname like ‘%明%’;
-
显示内连接:select * from 左表,右表 where 关联条件;
-
隐式内连接:select * from 左表 inner join右表 on 关联条件; (inner可省略)
-
左外连接:select * from 左表 left outer join 右表 on 关联条件; (outer可省略)
-
右外连接:select * from 左表 right outer join 右表 on 关联条件;(outer可省略)
常见的sql语句
于 2022-04-26 14:23:18 首次发布