CREATE DATABASE IF NOT EXISTS exercise DEFAULT CHARACTER SET UTF8 COLLATEUFT8_GENERAL_CI;USE exercise;CREATE TABLE IF NOT EXISTS stu_course (`number` INT ( 11 ) AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT ' 课程编号 ' ,name VARCHAR ( 20 ) NOT NULL COMMENT ' 课程名称 ' ,score FLOAT ( 5 , 2 ) NOT NULL COMMENT ' 学分 ') ENGINE = InnoDB CHARSET = UFT8 COMMENT = ' 课程表 ' ;
ALTER TABLE stu_score RENAME AS course;
ALTER TABLE course ADD `time` INT ( 3 ) NOT NULL COMMENT ' 学时 ' ;
ALTER TABLE course MODIFY score FLOAT ( 3 , 1 ) NOT NULL COMMENT ' 学分 ' ;
DROP TABLE IF EXISTS course;
DROP DATABASE IF EXISTS exercise;
-- 需要注意, VALUES 后的字段值必须与表名后的字段名一一对应INSERT INTO 表名 ( 字段名 1, 字段名 2, ..., 字段名 n ) VALUES ( 字段值 1, 字段值 2, ..., 字段值n ) ;-- 需要注意, VALUES 后的字段值必须与创建表时的字段顺序保持一一对应INSERT INTO 表名 VALUES ( 字段值 1, 字段值 2, ..., 字段值 n ) ;-- 一次性插入多条数据INSERT INTO 表名 ( 字段名 1, 字段名 2, ..., 字段名 n ) VALUES ( 字段值 1, 字段值 2, ..., 字段值n ) , ( 字段值 1, 字段值 2, ..., 字段值 n ) , ... , ( 字段值 1, 字段值 2, ..., 字段值 n ) ;INSERT INTO 表名 VALUES ( 字段值 1, 字段值 2, ..., 字段值 n ) , ( 字段值 1, 字段值 2, ..., 字段值n ) , ..., ( 字段值 1, 字段值 2, ..., 字段值 n ) ;
INSERT INTO course ( `number` , name, score, `time` ) VALUES ( 1 , 'Java 基础 ' , 4 , 40 ) ;INSERT INTO course VALUES ( 2 , ' 数据库 ' , 3 , 20 ) ;INSERT INTO course ( `number` , score, name, `time` ) VALUES ( 3 , 5 , 'Jsp' , 40 ) ;INSERT INTO course ( `number` , name, score, `time` ) VALUES ( 4 , 'Spring' , 4 , 5 ) , ( 5 ,'Spring Mvc' , 2 , 5 ) ;INSERT INTO course VALUES ( 6 , 'SSM' , 2 , 3 ) , ( 7 , 'Spring Boot' , 2 , 2 ) ;
UPDATE 表名 SET 字段名 1 = 字段值 1 [ , 字段名 2 = 字段值 2, ..., 字段名 n = 字段值 n ] [ WHERE 修改条件 ]
WHERE time > 20 && time < 40 ; <=> WHERE time > 20 and time < 40 ;
UPDATE course SET score = 4 , `time` = 15 WHERE name = ' 数据库 ' ;
DELETE FROM 表名 [ WHERE 删除条件 ] ;
DELETE FROM course WHERE `number` = 1 ;
-- 清空表中数据TRUNCATE [ TABLE ] 表名 ;
TRUNCATE course;
SELECT ALL /DISTINCT * | 字段名 1 AS 别名 1 [ , 字段名 1 AS 别名 1, ..., 字段名 n AS 别名 n ]FROM 表名 WHERE 查询条件
SELECT name FROM course WHERE `number` < 5 ;
SELECT score, `time` FROM course WHERE name = 'Java 基础 ' ;
SELECT * FROM course WHERE name IS NULL ;
SELECT * FROM course WHERE name IS NOT NULL ;
SELECT * FROM course WHERE score BETWEEN 2 AND 4 ;
SELECT * FROM course WHERE name LIKE '%v%' ;
SELECT * FROM course WHERE name LIKE 'J%' ;
SELECT * FROM course WHERE name LIKE '%p' ;
SELECT * FROM course WHERE `number` IN ( 1 , 3 , 5 ) ;
DROP TABLE IF EXISTS student;CREATE TABLE student (no BIGINT ( 20 ) AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT ' 学号,主键 ' ,name VARCHAR ( 20 ) NOT NULL COMMENT ' 姓名 ' ,sex VARCHAR ( 2 ) DEFAULT ' 男 ' COMMENT ' 性别 ' ,age INT ( 3 ) DEFAULT 0 COMMENT ' 年龄 ' ,score DOUBLE ( 5 , 2 ) COMMENT ' 成绩 ') ENGINE = InnoDB CHARSET = UTF8 COMMENT = ' 学生表 ' ;
INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 张三 ' , ' 男 ' , 20 ,59 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 李四 ' , ' 女 ' , 19 ,62 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 王五 ' , ' 其他 ' ,21 , 62 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 龙华 ' , ' 男 ' , 22 ,75 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 金凤 ' , ' 女 ' , 18 ,80 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 张华 ' , ' 其他 ' ,27 , 88 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 李刚 ' , ' 男 ' , 30 ,88 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 潘玉明 ' , ' 女 ' ,28 , 81 ) ;INSERT INTO student ( no , name, sex, age, score ) VALUES ( DEFAULT , ' 凤飞飞 ' , ' 其他 ' ,32 , 90 ) ;
SELECT ALL /DISTINCT * | 字段名 1 AS 别名 1 [ , 字段名 1 AS 别名 1, ..., 字段名 n AS 别名 n ]FROM 表名 WHERE 查询条件 GROUP BY 字段名 1 ,字段名 2,..., 字段名 n
SELECT * FROM student WHERE score > 80 GROUP BY sex;
SELECT * FROM student WHERE score BETWEEN 60 AND 80 GROUP BY sex, age;
SELECT COUNT ( * ) total FROM student WHERE score > 80 ;
SELECT COUNT ( * ) totalCount, SUM ( score ) totalScore FROM student WHEREscore < 60 ;
SELECT sex, AVG ( score ) avgScore FROM student GROUP BY sex;
SELECT MAX ( age ) FROM student;
SELECT MIN ( score ) FROM student;
SELECT ALL /DISTINCT * | 字段名 1 AS 别名 1 [ , 字段名 1 AS 别名 1, ..., 字段名 n AS 别名 n ]FROM 表名 WHERE 查询条件 GROUP BY 字段名 1 ,字段名 2,..., 字段名 n HAVING 筛选条件
SELECT * FROM student WHERE age BETWEEN 20 AND 30 GROUP BY sex HAVINGavg ( score ) > 74 ;
SELECT ALL /DISTINCT * | 字段名 1 AS 别名 1 [ , 字段名 1 AS 别名 1, ..., 字段名 n AS 别名 n ]FROM 表名 WHERE 查询条件 ORDER BY 字段名 1 ASC | DESC ,字段名 2 ASC | DESC ,..., 字段名 n ASC | DESC
SELECT * FROM student WHERE age BETWEEN 18 AND 30 ORDER BY score DESC , age ASC ;
SELECT ALL /DISTINCT * | 字段名 1 AS 别名 1 [ , 字段名 1 AS 别名 1, ..., 字段名 n AS 别名 n ]FROM 表名 WHERE 查询条件 LIMIT 偏移量 , 查询条数
SELECT * FROM student WHERE score >= 60 LIMIT 3 , 3 ;