MYSQL初级知识

一、连接数据库

mysql -uroot -p

二、数据库基础知识

数据库字段类型:

数值:

字符串:

日期:



  • 数据库相关命令:

创建数据库:create database [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];

删除数据库:drop database [if not exists] 数据库名;

使用数据库:use 数据库名;

查询当前数据库:select database();

  • 表格基础命令语法:

查询:select * from tables;

插入数据:insert into tables (字段1,字段2....) values (数值1,数值2...) ; #注意字段和数值必须一一对应

更改表格数据:update tables set 字段=数值 where 条件;

修改表格数据类型:alter table 表名 moddify 字段名 新数据类型;

修改字段和字段类型:alter table 表名 旧字段名 新字段名 类型 [comment 注释]

  • 命令示例

  • 1、查询有多少数据库

show database;
  • 2、使用指定数据库

use 'database';
  • 3、建立数据库

create table stu (
id int comment '编号',
name varchar(50) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment  '身份证号',
address varchar(10) comment  '城市',
entdata date comment '入学时间'
) comment '学生表';
  • 4、查询数据库中的表格

show table;
  • 5、插入数据

insert into stu  values (2,'谢逊','男',20,'123412789987654321','西安','2009-8-9'),
(3,'张三','男',25,'166412789987654321','西安','2009-8-9'),
(4,'李四','男',20,'123412789987654321','西安','2009-8-9'),
(5,'王五','男',20,'123412789987654321','武汉','2009-8-9'),
(6,'陈留','男',20,'123412789987654321','浙江','2009-8-9'),
(7,'张麻子','男',20,'123412649987654321','浙江','2009-8-9'),
(8,'孙琦','女',20,'123412789987654321','广东','2009-8-9'),
(9,'谢顶天','男',20,'123412789987654321','杭州','2009-8-9'),
(10,'张敏','女',20,'123412769987654321','贵阳','2009-8-9'),
(11,'严娇','女',20,'123412789987654321','北京','2009-8-9'),
(12,'小龙女','女',20,'123412789987654321','天津','2009-8-9'),
(13,'苏雨','男',20,'123412489987654321','北京','2009-8-9'),
(14,'方芳','女',20,'123412789987654321','重庆','2009-8-9'),
(15,'阿大','男',20,'123412789987654321','濮阳','2009-8-9'),
(16,'孟轲','男',20,'123412789987654321','广安','2009-8-9'),
(17,'王媛媛','女',19,'123412789987654321','西安','2009-8-9');
  • 6、修改数据

update stu set name='柳如烟', gender='女',idcard='12365479832148523X' where name='王语嫣';
update stu set entdata='2010-5-11' where id=7;
update stu set age=23 where id =8;
update stu set age=24 where id =12;
update stu set age=21 where id =10;
update stu set age=17 where id =5;
update stu set age=19 where id =16;
update stu set age=20 where id =13;

  • 7、查询数据

#查询id为6的所有数据
select * from stu where id=6;
​
#统计当前表格有多少数据
select count(*) from stu;
select count(id) from stu;
​
#统计表格中男女各有多少人
select gender,count(*) from stu group by gender;
​
#统计男女字段的平均年龄
select gender,avg(age) from stu group by gender;
​
#查询年龄在19-21之间的学生信息。
select * from stu where age between 19 and 21;
select * from stu where age >=19 and age<=21;
select * from stu where age in (19,20,21);
 
#查询年龄小于22的同学并按照地址分组,获取学生数量大于3个的地区
select address,count(*) from stu where age<22 group by address having count(*)>=3;
​
#按照年龄排序,多字段排序中间用, 分割
select * from stu order by age asc;
select * from stu order by  age desc;
​
#分页查询第一页数据,每页显示10条(起始页码-1*条数,条数)
select * from stu limit 0,10;
#分页查询第二页数据,每页显示10条
select * from stu limit 10,10;
​
  • 8、删除数据

delete from 表名 [where 条件];


文章作者: 锦轩
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 锦轩个人小站
软件类 服务器 MYSQL linux 系统 MYSQL
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝