文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

mysql基本语法

2015-03-11 10:02

关注

mysql基本语法

建库建表Demo
-- 建库
create DATABASE db_book;
use db_book;
-- 建表
CREATE TABLE t_bookType(
    id int primary key auto_increment,
    bookTypeName varchar(20),
    bookTypeDesc varchar(200)
);
CREATE TABLE t_book(
    id int primary key auto_increment,
    bookName varchar(20),
    author varchar(10),
    price decimal(6,2),
    bookTypeId int,
    constraint `fk` foreign key (`bookTypeId`) references `t_bookType`(`id`)
);
-- 查看表结构
desc t_bookType;
-- 查看表ddl(建表语句)
show create table t_bookType;
-- 重命名表
alter table t_book rename t_book2;
View Code 建立单表
-- 建表
create table `t_student` (
    `id` double ,
    `stuName` varchar (60),
    `age` double ,
    `sex` varchar (30),
    `gradeName` varchar (60)
); 
-- 插入记录
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("1","张一","23","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("2","张二","25","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("3","张三","23","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("4","张四","22","","三年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("5","张五","21","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("6","李一","26","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("7","李二","20","","三年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("8","李三","21","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("9","李四","22","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("10","李五","25","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("11","小黑","21",NULL,"二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("12","小白","23","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("13","小红","24",NULL,"二年级");
View Code 简单的单表查询
-- 查询
SELECT id,stuName,age,sex,gradeName FROM t_student ;
SELECT * FROM t_student;
SELECT * FROM t_student WHERE id=1;
SELECT * FROM t_student WHERE age>22;
-- in 相当于集合吧,别和between混淆
SELECT * FROM t_student WHERE age IN (21,22,23);
SELECT * FROM t_student WHERE age NOT IN (21,23);
-- [21,24]
SELECT * FROM t_student WHERE age BETWEEN 21 AND 24;
SELECT * FROM t_student WHERE age NOT BETWEEN 21 AND 24;
-- 模糊查询
SELECT * FROM t_student WHERE stuName LIKE "张三";
SELECT * FROM t_student WHERE stuName LIKE "张%";
SELECT * FROM t_student WHERE stuName LIKE "%张%";
-- 交集
SELECT * FROM t_student WHERE gradeName="一年级" AND age=23;
-- 并集
SELECT * FROM t_student WHERE gradeName="一年级" OR age=23;
-- DISTINCT去重
SELECT DISTINCT gradeName FROM t_student;
-- 升序
SELECT * FROM t_student ORDER BY age ASC;
-- 降序
SELECT * FROM t_student ORDER BY age DESC;
-- 分组查询
SELECT gradeName,GROUP_CONCAT(stuName) FROM t_student GROUP BY gradeName;
-- 分页查询(index,size)
SELECT * FROM t_student LIMIT 2,5;
View Code
再建单表
create table `t_grade` (
    `id` int ,
    `stuName` varchar (60),
    `course` varchar (60),
    `score` int 
); 
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("1","张三","语文","91");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("2","张三","数学","90");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("3","张三","英语","87");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("4","李四","语文","79");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("5","李四","数学","95");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("6","李四","英语","80");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("7","王五","语文","77");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("8","王五","数学","81");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("9","王五","英语","89");
View Code
-- 聚合查询,还是分组聚合比较多
SELECT COUNT(*) FROM t_grade;
SELECT stuName,COUNT(*) FROM t_grade GROUP BY stuName;
SELECT stuName,SUM(score) FROM t_grade GROUP BY stuName;
SELECT stuName,AVG(score) FROM t_grade WHERE stuName="张三";
SELECT stuName,AVG(score) FROM t_grade GROUP BY stuName;
View Code 建立无外键的俩表
USE `db_book`;
DROP TABLE IF EXISTS `t_book`;
CREATE TABLE `t_book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bookName` varchar(20) DEFAULT NULL,
  `price` decimal(6,2) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  `bookTypeId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
insert  into `t_book`(`id`,`bookName`,`price`,`author`,`bookTypeId`) values (1,"Java编程思想","100.00","埃史尔",1),(2,"Java从入门到精通","80.00","李钟尉",1),(3,"三剑客","70.00","大仲马",2),(4,"生理学(第二版)","24.00","刘先国",4);
DROP TABLE IF EXISTS `t_booktype`;
CREATE TABLE `t_booktype` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bookTypeName` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

insert  into `t_booktype`(`id`,`bookTypeName`) values (1,"计算机类"),(2,"文学类"),(3,"教育类");
View Code 多表查询(俩表)
-- 笛卡尔积
SELECT * FROM t_book,t_bookType;
SELECT * FROM t_book,t_bookType WHERE t_book.bookTypeId=t_bookType.id;
SELECT bookName,author,bookTypeName FROM t_book,t_bookType WHERE t_book.bookTypeId=t_bookType.id;
SELECT tb.bookName,tb.author,tby.bookTypeName FROM t_book tb,t_bookType tby WHERE tb.bookTypeId=tby.id;
-- 返回左表所有记录,哪怕右表为空
SELECT * FROM t_book LEFT JOIN t_bookType ON t_book.bookTypeId=t_bookType.id;
-- 返回右表所有记录,哪怕左表为空
SELECT * FROM t_book RIGHT JOIN t_bookType ON t_book.bookTypeId=t_bookType.id;
SELECT tb.bookName,tb.author,tby.bookTypeName FROM t_book tb LEFT JOIN t_bookType tby ON tb.bookTypeId=tby.id;
SELECT tb.bookName,tb.author,tby.bookTypeName FROM t_book tb,t_bookType tby WHERE tb.bookTypeId=tby.id AND tb.price>70;
View Code 建表子查询
create table `t_pricelevel` (
    `id` int ,
    `priceLevel` int ,
    `price` float ,
    `description` varchar (300)
); 
insert into `t_pricelevel` (`id`, `priceLevel`, `price`, `description`) values("1","1","80.00","价格贵的书");
insert into `t_pricelevel` (`id`, `priceLevel`, `price`, `description`) values("2","2","60.00","价格适中的书");
insert into `t_pricelevel` (`id`, `priceLevel`, `price`, `description`) values("3","3","40.00","价格便宜的书");
-- 子查询
SELECT * FROM t_book WHERE booktypeId IN (SELECT id FROM t_booktype);
SELECT * FROM t_book WHERE booktypeId NOT IN (SELECT id FROM t_booktype);
SELECT * FROM t_book WHERE price>=(SELECT price FROM t_pricelevel WHERE priceLevel=1);
SELECT * FROM t_book WHERE EXISTS (SELECT * FROM t_booktype);
SELECT * FROM t_book WHERE NOT EXISTS (SELECT * FROM t_booktype);
SELECT * FROM t_book WHERE price>= ANY (SELECT price FROM t_pricelevel);
SELECT * FROM t_book WHERE price>= ALL (SELECT price FROM t_pricelevel);
View Code

博客使用的mysql实例均来自http://www.java1234.com/

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯