-- 创建数据库
CREATE DATABASE mybook;
CREATE DATABASE mybook;
-- 删除数据库
DROP DATABASE mybook;
CREATE DATABASE book;
DROP TABLE t_st;-- 删除表
DROP DATABASE mybook;
CREATE DATABASE book;
DROP TABLE t_st;-- 删除表
-- 创建表
CREATE TABLE t_stu(
id INT PRIMARY KEY AUTO_INCREMENT,
studentName VARCHAR(20),
sex VARCHAR(20),
birthday DATE,
tel VARCHAR(20)
CREATE TABLE t_stu(
id INT PRIMARY KEY AUTO_INCREMENT,
studentName VARCHAR(20),
sex VARCHAR(20),
birthday DATE,
tel VARCHAR(20)
);
-- 查询表中所有的数据
SELECT *FROM t_stu; -- *表示查询所有的列 *可以用列名表示,多个列名之间用,隔开
-- 添加数据
INSERT INTO t_stu(studentName,sex,birthday,tel)
VALUES('宇文成都','男','1604-09-20','13800000000'),
('李四','男','1999-10-23','12345623233'),
('王麻子','男','1994-02-14','134234288'),
('王五','男','1994-02-14','134234288'),
('赵六','女','1989-02-16','13442363555'),
('周八','女','1993-09-14','18432654755'),
('土肥圆','男','1924-02-14','18426547846'),
('山本五十六','女','1921-02-16','13432454632'),
('岗村宁次','女','1915-09-14','15647566467'),
('张三','男','1980-09-20','18800000000'),
('宇文化及','男','1615-09-14','15647566467'),
('李渊','男','1623-09-20','18800000000'),
('小李子','男','1623-09-20','13800000000');
INSERT INTO t_stu(studentName,sex,birthday,tel)
VALUES('王6','男','1989-09-20','15900000000');
SELECT *FROM t_stu; -- *表示查询所有的列 *可以用列名表示,多个列名之间用,隔开
-- 添加数据
INSERT INTO t_stu(studentName,sex,birthday,tel)
VALUES('宇文成都','男','1604-09-20','13800000000'),
('李四','男','1999-10-23','12345623233'),
('王麻子','男','1994-02-14','134234288'),
('王五','男','1994-02-14','134234288'),
('赵六','女','1989-02-16','13442363555'),
('周八','女','1993-09-14','18432654755'),
('土肥圆','男','1924-02-14','18426547846'),
('山本五十六','女','1921-02-16','13432454632'),
('岗村宁次','女','1915-09-14','15647566467'),
('张三','男','1980-09-20','18800000000'),
('宇文化及','男','1615-09-14','15647566467'),
('李渊','男','1623-09-20','18800000000'),
('小李子','男','1623-09-20','13800000000');
INSERT INTO t_stu(studentName,sex,birthday,tel)
VALUES('王6','男','1989-09-20','15900000000');
-- 删除ID=1的学生
DELETE FROM t_stu WHERE ID=13;
DELETE FROM t_stu WHERE ID=13;
-- 删除姓李的学生
DELETE FROM t_stu WHERE studentName LIKE '%李%';
DELETE FROM t_stu WHERE studentName LIKE '%李%';
-- 修改电话
UPDATE t_stu SET tel='10010' WHERE studentName='土肥圆';
UPDATE t_stu SET tel='1230000000' WHERE studentName='山本五十六';
UPDATE t_stu SET tel='10010' WHERE studentName='土肥圆';
UPDATE t_stu SET tel='1230000000' WHERE studentName='山本五十六';
-- 同时修改电话和生日
UPDATE t_stu SET tel = '1008611',birthday = '2000-10-10 ' WHERE studentName = "山本五十六";
UPDATE t_stu SET tel = '1008611',birthday = '2000-10-10 ' WHERE studentName = "山本五十六";
-- 查询
SELECT * FROM t_stu WHERE studentName LIKE '%山%';
SELECT * FROM t_stu WHERE tel LIKE '18%';
SELECT * FROM t_stu WHERE studentName LIKE '%山%';
SELECT * FROM t_stu WHERE tel LIKE '18%';
-- 查询
SELECT *FROM t_stu WHERE studentName LIKE '王%' AND sex='男';
SELECT *FROM t_stu WHERE birthday >'1950-01-01' AND birthday<'2000-01-01';
SELECT *FROM t_stu WHERE studentName LIKE '王%' AND sex='男';
SELECT *FROM t_stu WHERE birthday >'1950-01-01' AND birthday<'2000-01-01';
-- 查询所有的移动用户
SELECT *FROM t_stu WHERE tel LIKE '13%' OR tel LIKE '15%';
SELECT *FROM t_stu WHERE tel LIKE '13%' OR tel LIKE '15%';
-- 查询第三条到第五条记录
SELECT * FROM t_stu LIMIT 3,3;-- 第一个参数表示从第几条记录开始,第二个参数表示查询几条记录
SELECT * FROM t_stu LIMIT 3,3;-- 第一个参数表示从第几条记录开始,第二个参数表示查询几条记录
-- 按年龄排序 asc升序 desc 降序;
SELECT*FROM t_stu ORDER BY birthday DESC LIMIT 0,1;
SELECT*FROM t_stu ORDER BY birthday DESC LIMIT 0,1;
INSERT INTO t_stu(studentName,sex,birthday)VALUES('刘军','男','1992-09-10'),
('马大炮','男','1989-07-20');
-- 查询没有手机的学生
SELECT * FROM t_stu WHERE tel IS NOT NULL;
SELECT * FROM t_stu WHERE tel IS NOT NULL;
-- 查询ID为3和5,8的学生
SELECT *FROM t_stu WHERE id IN (3,5,8); -- in 在...范围之内
-- 查询所有学生的性别 DISTINCT 去除重复
SELECT DISTINCT sex FROM t_stu;
SELECT DISTINCT sex FROM t_stu;