mysql最基本语句:

进入sql:
1、启动mysql:net start mysql
2、进入mysql : mysql -uroot -proot
1、对库操作:
3、创建数据库:create database 库名;
4、查看数据库:show databases;
5、切换数据库:use 库名;
5、删除数据库:drop database 库名;
6、修改数据库:alter database change 旧库名 新库名;
2、对表操作:
1、创建表:create table 表名(字段名字 类型(长度),其他字段);
2、修改表的名字:rename table 旧表名 to 新表名;
3、表里增加字段:alter table 表名 add 新字段名字 字段类型和长度;
4、表里删除字段:alter table 表名 drop 字段名字;
5、表里修改字段名:alter table 表名 change 旧字段名 新字段名 类型(长度);
6、表里修改字段类型:也是用change,自己思考。

3、DDL(增删改)

	1、增加一行数据:insert into 表名 values(所有字段要添加的值);

	2、增加一行数据(指定字段值):insert into 表名(字段名,..,) values(指定字段要添加的值);

	3、增加多行数据:(提示每条数据用括号括着,每个括号用逗号隔开)
	   
		insert into 表名 values(),(),(),(),();插入5条数据

	3、修改指定字段的值:update 表名 set 字段名="修改的值" where sid=1;

	4、删除一行数据:delete from 表名 where sid=1;

4、DQL:

	1、查询单行或者多行数据:select * from 表名 where sid=1;

	2、查询指定字段的一列值:select sid,sname from 表名;

	3、查询区间的数据:select  *  from  表名   where  分数字段名 between    60 and  90;

	4、查询或者数据:select  *   from   表名  where  分数字段名 = 60 or  分数字段名=80;

	5、判断当前字段是否有值:is  null; 注意ename=""/"bull",并不属于空。

	6、分页查询:select  *  from  表名 limit 当前索引((当前页-1)*页量),第二个参数是指你需要取的数据条数。
	
		select * from student limit 0,3;//用分页查询前三条数据。

	7、聚合函数:max、min、svg、sum,一般都配合as关键字写。

		1、select max(sresult) as "最高分" from student;//这里用as写了个别名,显示清晰明了。

		2、其他min、svg、sum格式也一样的就不多写了。

	8、过滤相同班级:

		select distinct 字段名 from student;,(注意distinct不能放其他东西)。
	
	9、查询每种科目最高分:

		select gradeName,max(sresult) as "最高分" from student group by gradeName;//代表分组的意思。

	10、按照成绩的高低查询所有:

		select * from student order by sresult desc;//这是desc是降序,asc是升序。

	11、模糊查询:

		1、like : 查询姓名包含某个字母的都输出

		select * from student where sname like "%R%";//查询名字包含R字母的那行数据,注意双引号里不能有空格。			
		2、"__" :   查询名字是三个字母的查出来
	
		select * from student where sname like "___";//查询名字是三个字符的那行数据。

	12、时间查询:

		1: now(): 查询当前时间完整格式:	select now();	
					
		2、currebt_date;  查询当前日期 2018-09-04
	
		3、current_time; 查询当前时分秒 

猜你喜欢

转载自blog.csdn.net/weixin_42334396/article/details/83176019
今日推荐