mysql数据库基础知识

Posted 帅涛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库基础知识相关的知识,希望对你有一定的参考价值。

/*新建表*/
CREATE TABLE order01(
    oid INT,
    productname VARCHAR(10),
    otime DATETIME,
    money DOUBLE
)
/*添加字段*/
ALTER TABLE order01 ADD COLUMN odesc VARCHAR(50);
/*删除字段*/
ALTER TABLE order01 DROP COLUMN money;
/*修改字段类型*/
ALTER TABLE order01 MODIFY COLUMN otime VARCHAR(20);
/*修改字段名称*/
ALTER TABLE order01 CHANGE COLUMN otime ordertime VARCHAR(20);
/*修改表名*/
ALTER TABLE order01 RENAME TO order02;
/*新增数据*/
INSERT INTO order02 VALUES(1,‘口红‘,‘2018-11-13‘,‘骗女人钱的‘);
INSERT INTO order02(oid,productname,odesc) VALUES(2,‘手表‘,‘骗男人钱的‘);
/*修改数据*/
UPDATE order02 SET ordertime=‘2018-12-12‘ WHERE oid=2;
/*修改多个字段*/
UPDATE order02 SET productname=‘面膜‘,ordertime=‘2018-11-11‘ WHERE oid=1;
/*删除数据*/
DELETE FROM order02 WHERE oid=2;
/*全表删除*/
DELETE FROM order02;
TRUNCATE TABLE order02;
/*查询所有字段*/
SELECT*FROM order02;
/*查询指定字段*/
SELECT oid,productname,odesc FROM order02;
/*查询时添加常量列*/
SELECT oid AS‘编号‘,productname FROM order02;
/*新建成绩表*/
CREATE TABLE score(
    sid INT,
    sname VARCHAR(10),
    ssex CHAR(2),
    html DOUBLE,
    js DOUBLE,
    jquery DOUBLE
);
DROP TABLE score;
SELECT*FROM score;
INSERT INTO score VALUES(1,‘李全‘,‘男‘,88,55,99);
INSERT INTO score VALUES(2,‘张超‘,‘女‘,55,77,94);
INSERT INTO score VALUES(3,‘赵涛‘,‘男‘,11,99,22);
INSERT INTO score VALUES(4,‘冯旭‘,‘女‘,7,8,9);
/*查询时合并列*/
SELECT sname,(html+js+jquery) AS ‘总成绩‘ FROM score;
/*查询时去除重复列*/
SELECT DISTINCT ssex FROM score;
SELECT DISTINCT(ssex) FROM score;
/*逻辑条件查询*/
SELECT * FROM score WHERE sid=1 AND ssex=‘男‘;
SELECT * FROM score WHERE sid=2 OR ssex=‘女‘;
/*比较条件查询*/
SELECT * FROM score WHERE html>=60 AND js<=80;
SELECT * FROM score WHERE html>=60 AND html<=100;
SELECT * FROM score WHERE html BETWEEN 60 AND 100;
SELECT * FROM score WHERE <>60 AND js>8;
INSERT INTO score(sid,sname,ssex,html)VALUES(5,‘高炜城‘,‘男‘,80);
INSERT INTO score(sid,sname,ssex,html)VALUES(6,‘张三‘,‘‘,80);
/*判断是否为null*/
SELECT * FROM score WHERE js IS NOT NULL;
/*判断是否为空字符串*/
SELECT * FROM score WHERE ssex <> ‘‘;
SELECT * FROM score WHERE ssex = ‘‘;
/*模糊查询*/
SELECT * FROM score WHERE sname LIKE ‘张%‘;
SELECT * FROM score WHERE sname LIKE ‘__‘;
SELECT * FROM score WHERE sname LIKE ‘%张%‘;
SELECT SUM(js) AS ‘js成绩‘ FROM score;
SELECT AVG(html) AS ‘html平均成绩‘ FROM score;
SELECT MAX(jquery) FROM score;
SELECT MIN(js) FROM score;
/*求本班多少人*/
SELECT COUNT(*) FROM score;
/*分页查询*/   /*起始行=(当前页-1)*每页显示的条数*/
/*第一页*/
SELECT * FROM score LIMIT 0,1;
/*第二页*/
SELECT * FROM score LIMIT 2,1;
/*查询排序*/
SELECT * FROM score ORDER BY html ASC;
SELECT * FROM score WHERE ssex=‘男‘ ORDER BY html DESC;
SELECT * FROM score ORDER BY js ASC,jquery DESC;
/*分组查询*/
SELECT ssex,COUNT(*) FROM score GROUP BY ssex;
/*查询总人数大于2的性别(HAVING)*/ /*按性别分组*/
SELECT ssex FROM score GROUP BY ssex
HAVING COUNT(*)>2;






























































































以上是关于mysql数据库基础知识的主要内容,如果未能解决你的问题,请参考以下文章

9月23日 | MySQL基础入门-mysql教程-数据库实战(MySQL基础+MySQL高级+MySQL优化+作业题)

MySQL数据库基础知识

专题九.Mysql数据库编程基础知识

mysql数据库之mysql基础命令

MySQL入门基础一-Mysql的基础体系架构

Mysql基础1