MySql数据库 基本命令

本文详细介绍了MySQL数据库的创建、删除、修改以及数据表的管理,包括学生表、课程表和成绩表的建立、删除、查询和更新操作。此外,还涉及到聚合函数、时间函数、字符串函数等数据库操作,以及数据备份与导入。内容涵盖了数据库的基础管理和进阶应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-- 建库
create database tt;
drop database tt;
create database if not exists tt;
alter database tt default CHARACTER set 'utf8';
show create database tt;
-- 建立学生表
use tt
create table if not exists `stu`(
sid int(11) UNSIGNED PRIMARY KEY auto_increment,
`name` VARCHAR(20) not null,
age int,
sex enum('m','f','mid') DEFAULT 'mid',
birth datetime not null default now(),
email VARCHAR(50),
addr VARCHAR(100) default '北京',
tel VARCHAR(11)
)

-- 创建课程表
create table if not exists course( 
cid int(11) UNSIGNED PRIMARY KEY auto_increment,
cname varchar(20) not null
)

-- 创建成绩表
create table if not exists score(
sid int(11) UNSIGNED not null,
cid int(11) UNSIGNED not null,
cj int(11) UNSIGNED not null,
PRIMARY key(sid,cid),
FOREIGN key(sid) REFERENCES stu(sid) on delete CASCADE on UPDATE CASCADE,
FOREIGN KEY(cid) REFERENCES course(cid) on DELETE CASCADE on update CASCADE
)ENGINE=INNODB DEFAULT CHARSET=utf8

-- 删除数据表
drop table stu 
drop table course 
drop table score

-- 查看表结构
show tables;
desc stu;
-- 修改表结构
alter table stubak add beizhu varchar(200) COMMENT'备注';
-- 开头增加一个字段
alter table stu add xxx varchar(20) FIRST;
-- 中间位置增加一个字段
alter TABLE stubak add mr varchar(1) not null DEFAULT'否' after birth;
-- 删除字段 xxx
alter table stu drop xxx
-- 修改字段  change MODIFY
alter table stubak CHANGE beizhu bz VARCHAR(100);
select * FROM stu;
alter table stubak MODIFY mr bit(1) not null DEFAULT 0;
desc stu
select * FROM stu;
-- 部分字段增加记录
insert into stu(name,age,sex,birth,email,tel) VALUES('张三',20,'m','2002-1-1','zs@abc.com','15512687946');
insert into stu(name,age,sex,birth,addr,tel) VALUES('李四',30,'m','1992-1-1','上海','15512687947');
-- 全部字段增加记录
insert into stu VALUES(3,'王武',32,'f','1999-4-3',1,'ww@bcd.com','上海','123486545','王武')
insert into stu VALUES(4,'老毕灯',80,'f','1942-8-2',1,'lbd@bcd.com','越南','12348545','谢正阳')
insert into stu VALUES(5,'李六',90,'f','1932-8-18',1,'ll@bcd.com','上海','1234865045','李六')
insert into stu VALUES(6,'詹姆斯',40,'f','1982-5-13',1,'zms@bcd.com','武汉','123476545','詹姆斯')
insert into stu VALUES(7,'库里',38,'f','1980-5-6',1,'kl@bcd.com','上海','123486565','kuli')

select * FROM stu;
select * FROM score;
select * FROM course;
-- 插入课程表
insert into course(cname) values('数据库'),('数据结构')
insert into course(cname) values('安全运维'),('完全开发'),('安服');
-- 插入成绩表
insert into score values(1,1,89),(1,2,56),(2,3,66),(2,2,34),(2,5,56),(4,1,90),(4,4,67),(4,2,56)
-- 删除记录
delete from stu where sid=5
-- 修改记录
use tt
update stu set name='张三1' where sid=1;
update stu set age='1000' where sid=4;
-- 设置别名
select * FROM stu where sid=4;
select * FROM stu as tt where tt.sid=4;
select sid as 学号,name as 姓名 from stu;

-- 查询
-- 查询所有姓名为张开头的学生
select * from stu where name like '老%';
-- 查询年龄在 25 岁以上的学生
select * from stu where age>500;
-- 查询家住北京和上海的学生
select * from stu where addr='上海' or addr='北京';
select * from stu where addr in('上海','北京');
-- 没有留下邮箱的人
select * from stu where email is null;
-- 又留下邮箱的人
SELECT * from stu where email is not null;
-- 查询张三的信息
select * from stu where name='张三1';
-- 查新sid为1号的学生信息
select * from stu where sid=1;
-- 查询已婚学生的信息
select * from stu where mr=1; 
-- 查询邮箱发给为def的学生信息
select * from stu where email like '%bcd%';
-- 查询邮箱首字母为a或b的学生信息
select * from stu where email like 'a%' or email like 'b%';
-- 查询成绩,按照降序排列 默认是升序
select * from score order by cj desc;
-- 考第一名的学生成绩 limit 0,1 从位置 0 开始,取一个
select * from score order by cj desc limit 0,1;
select * from score order by cj desc limit 0,2;
select * from score order by cj desc limit 1,1;

-- 聚合函数
-- 总共多少学生
SELECT count(*) as 学生总数 from stu;
-- 求总分
select sum(cj) as 总分 from score;
-- 平均分
SELECT avg(cj) as 平均分 from score;
-- 找最高分
select max(cj) as 最高分 from score;
-- 找最低分
SELECT min(cj) as 最低分 from score;
-- 男生多少人 女生多少人 
select sex as 性别,count(*) as 人数  from stu GROUP BY sex; 
-- 男生人数
select sex as 性别,count(*) as  人数 from stu group by sex having sex='m';
select sex as 性别,count(*) as  人数 from stu where sex='m';
-- 每个学生的平均成绩
select sid as 学号,avg(cj) as 平均分 from score
GROUP BY sid;
-- 求学号为 2 的学生的平均成绩
select sid,avg(cj) from score 
group by sid 
having sid=2;
-- 每门课程的最高分是多少分
select cid,max(cj) from score 
group by cid
-- 学生分组显示方式 GROUP_CONCAT()
SELECT sid,GROUP_CONCAT(cid),GROUP_CONCAT(cj order by  cj desc SEPARATOR '') from score
group by sid
-- 每个人平均分,从大到小排
select sid,avg(cj)from score
group by sid
order by avg(cj) desc
-- 函数
-- 时间函数
select now()
select sysdate();
select curtime()
select curtime(2)
-- curtime 当前时间
select curtime()
select curtime(2)
-- curdate 当前日期
select curdate()
select curdate()+2;
select CURRENT_TIME;
select CURRENT_USER;
SELECT now(),date(now());
SELECT now(),time(now());
SELECT now(),quarter(now());
SELECT now(),month(now()); -- 月
select now(),WEEK(now());
SELECT now(),weekday(now()); 
SELECT now(),day(now()); -- 日
SELECT now(),hour(now()); -- 小时 
SELECT now(),minute(now()); -- 分钟 
SELECT now(),second(now()); -- 秒 
SELECT now(),microsecond(now()); -- 微秒
-- 找出出生在 1982 年的学生信息
select * from stu where year(birth)='1982';
-- 找出所有的 90 后
select * from stu where year(birth) BETWEEN 1990 and 1999
select * from stu where year(birth) >=1990 and year(birth) <=1999 

select length('sql课程') -- 返回 9
select CHAR_LENGTH('sql课程') -- 返回 5
select right('买买提,土尔松',3)
select left('买买提,土尔松',3)
-- concat 字符串连接
select CONCAT('买买提',',','土尔松')
-- 将数据库中所有姓老的改成姓谢的
update stu set name=concat('谢',right(name,CHAR_LENGTH(name)-1)) where name like '老%';
select ORD('2')
select ord('abc')
select mid('abcd',2,2)
select sleep(2);
-- union 结果集列数一样
select 1,2 union select * from course

-- 备份 load data outfile 
select * from stu into OUTFILE '/tmp/stubak.txt'
show DATABASES

create table if not exists `stubak`(
sid int(11) UNSIGNED PRIMARY KEY auto_increment,//
`name` VARCHAR(20) not null,
age int,
sex enum('m','f','mid') DEFAULT 'mid',
birth datetime not null default now(),
email VARCHAR(50),
addr VARCHAR(100) default '北京',
tel VARCHAR(11)
)

load data infile '/tmp/stubak.txt' into table stubak;
select * from stubak

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值