如何在cmd终端操作MySql数据库

如何在cmd终端操作MySql数据库?
方法如下:
1.启动mysql服务器:net start mysql;
2.停止mysql服务器:net stop mysql;
3.连接mysql服务器:mysql -u root -h 127.0.0.1 -p
4.断开mysql服务器:quit、exit、\q
5.创建数据库:create database student; 或 create schema student;
6.查看数据库:show databases;
7.切换(使用,要操作的数据库):use 数据库名;
8.删除数据库:drop database student;
9.创建表:create table 表名(列名 类型,列名 类型,列名 类型);
10.查询数据库下所有的表:show tables;
11.查询表的结构:desc 表名、show columns from 表名 from 数据库名;
Show columns from 数据库名.表名;
12.创建指定字符集的数据库:create database 数据库名
Character set=gbk;
13.创建数据库前判断数据库名是否存在:create database if not exists 数据库名;
14.筛选‘db_’开头的数据库:show databases like ‘db_%’;
15.添加字段:alter table 表名 add(字段 类型,字段 类型);
16.删除字段:alter table 表名 drop 字段,drop 字段;
17.修改字段类型:alter table 表名 modify 字段 类型;
18.修改字段名:alter table 表名 change 旧字段名 新字段名 新字段类型;
19.修改表名:alter table 旧表名 rename as 新表名;
20.删除数据表:drop table 表名;
21.向表中插入数据:insert into 表名 values(1001,”张三”,25,”男”);
insert into 表名 set id=1001,xm=”张三”,age=25,sex=”男”;
22.修改表中记录:update 表名 set age=24 where id=1001;
23.删除表中特定数据:delete from 表名 where id=1001;
24.删除表中所有数据:truncate table 表名;
25.数据库的备份:mysqldump -uroot -p123 数据库名>c:\123.sql
26.数据库的恢复:mysql -uroot -p123 数据库名<c:\123.sql
27.查询所有字段: select * from 表名;
28.查询指定字段:SELECT 字段名,字段名 FROM 表名;
29.查询指定数据:select * from 表名 where xm=“张三”;
30.在集合范围内查询(关键字 in):select * from 表名 where id in(1001,1002,1005);
31.在范围内查询(between…… and…)select * from 表名 where age between 20 and 30;
32.空值查询: select * from stu where li_lun is null;
33.带关键字and多条件查询:select * from stu where li_lun>90 and ji_neng>90;
34.带关键字 or 多条件查询:select * from stu where id=1001 or li_lun>90;
35.去重查询:select distinct 字段名 from 表名;
36.列运算:1.数值计算:select 字段名+字段名 from 表名;
2.字符串连接:select concat (li_lun,”分”) from stu;
3.转换null值:select ifnull(li_lun,0)+ji_neng from stu;
4.给列起别名:select ifnull(li_lun,0)+ji_neng as “总分” from stu;(as可省略)
37.主键(primary key)
修改表时指定主键:alter table 表名 add primary key(id);
删除主键:alter table stu drop primary key;
38.主键自增长(primary key auto_increment):(此时主键必须设置为int)
修改表时设置主键自增长:alter table stu change sid sid int auto_increment;
修改表时删除主键自增长:alter table stu change sid sid int;
39.非空(not null)
添加非空约束:alter table 表名 modify 列名 列类型 not null;
修改非空约束:alter table 表名 modify 列名 列类型 null;
删除非空约束:alter table 表名 modify 列名 列类型;
40.唯一(unique)
添加唯一约束:alter table 列名 add unique(列名称);
修改唯一约束:alter table 表名 modify 列名称 列类型;
删除唯一约束:alter table 表名称 drop index 列名称;
41.默认值(default)
添加默认值约束:alter table 表名 modify 列名 列类型 not null default “默认值”;
42.外键(foreign key)
Constraint 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;
43.升序:select * from student order by age asc;
44.倒序:select * from student order by age desc;
45.分组排序求个数:select count(列名)from 表名 group by 分组的列;
46.模糊查询: __表示当的任意字符
% 表示任意长度的任意字符
列如:查询姓张的所有同学的记录:select * from student where sname like “张%”;
查询姓名中第二个字为“亮”的记录:select *from student where sname like “_亮%”;
查询姓名中包含“明”字的记录:select * from student where sname like “%明%”;
47.显示内连接:select * from 左表,右表 where 关联条件p.id=u.uid;
48.隐式内连接:select * from 左表 inner join 右表 on关联条件;(inner可省略)
49.左外连接:select *from 左表 left outer join 右表 on关联条件;(outer可省略)
50.右外连接:select * from 左表 right outer join 右表 on 关联条件;(outer可省略)
51.查询表结:DESCRIBE表名;
52.查询指定字段表结构:describe 表名 字段名;
53.限制查询:select * from 表名 字段名 limit 2;/只要前两条数据/
54.限制查询:select * from 表名 字段名 limit 2,2;/从第二条数据开始要两条数据/
55.Having函数的使用:having只能跟着group by后
例:select sum(字段名)from group by(字段名) having 字段名>9000;
56.查询各专业最高分的学生记录。(子查询)
SELECT * FROM student s JOIN major m ON s.smid=m.midd WHERE
score=ANY(SELECT MAX(score) FROM student GROUP BY smid );
57.Exists 存在的意思

  1. 查询出所有学生的详细信息,按分数降序排序,如果分数相同,按学生学号升序排序。
    SELECT * FROM studentt ORDER BY score DESC,sid DESC;
  2. 合并查询uninon 合并查询结构并去重 union all合并查询不去重
    列:select * from 表1 union(或者)union select 字段名 from 表名 ;
    60.-- 带关键字in的子查询,查找与霍元甲成绩和专业相同的学生
    61.SELECT * FROM student WHERE (sid,score,enrol)IN(SELECT sid,score,enrol FROM student WHERE sname=“霍元甲”);

63.-- 查找出分数高于霍元甲的同学的全部信息
64.SELECT * FROM student s JOIN major m ON s.sid=m.id WHERE score>(SELECT score FROM student WHERE sname=“霍元甲”);
65.
66.-- 如果表中存在小明同学就输出表中所有数据,否则不输出任何数据
67.SELECT * FROM student WHERE EXISTS(SELECT * FROM student WHERE sname=“小明”);
68.
69.-- 如果表中存在霍元甲同学就输出表中所有数据,否则不输出任何数据
70.SELECT * FROM student WHERE EXISTS(SELECT * FROM student WHERE sname=“霍元甲”);
71.ANY 满足子查询条件之一 ALL 满足全部子查询条件
72.-- 查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。【考核知识点:子查询】
73.SELECT deptno,dname,job,COUNT(deptno) FROM emp e JOIN dept d ON e.deptno=d.id
74.GROUP BY deptno HAVING COUNT(deptno)>=1;
75.Alter database 库名 charset latinl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值