数据库及表操作
// 1. 创建数据库
create database myDatabase character set utf8
// 2. 使用数据库
use myDatabase
// 3. 创建表
create table users (
uid int primary key auto_increment,
name varchar(20),
address varchar(200)
)
// 4. 显示所有表
show tables
// 5. 查看表格式
desc users
// 6. 删除表
drop table users
添加表、添加字段
// 1. 添加列, 添加字段
alter table users add tel int
// 2. 修改列信息(列名, 数据类型, 约束)
alter table users modify name varchar(50)
alter table users change name uname double
rename table users to users1
// 3. 删除列
alter table users drop uname
// 4. 修改字符集
alter table users character set gbk
添加数据
// 0. 创建表
CREATE TABLE product (
id INT PRIMARY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
price DOUBLE
)
// 1. 添加数据
insert into product (id, name, price) values
(1, '电脑', 9999.99),
(2, '手机', 8888.00),
(3, '平板', 4888.00),
// 可以不传主键, 因为会自增, 推荐使用不传主键的方法
insert into product (name, price) values
('电脑', 9999.99),
('手机', 8888.00),
('平板', 4888.00),
// 注意对应的问题 个数, 顺序, 数据类型等都要对应不要写错
更新数据
// 1. 更新数据
update product set price = 12899 where id = 2
update product set name = '笔记本', price = 12399 where id = 1
update product set price = 10000 where id = 1 or id = 3
删除数据
// 1. 删除 id 为 2 的所有数据
delete from product where id = 2
// 2. 删除整个表的数据
delete table product
查询数据
// 0. 创建表
CREATE TABLE accounts (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(200),
money DOUBLE
)
// 1. 插入数据
INSERT INTO accounts (id, name, money) values
(1, '吃饭支出', 240),
(2, '工资收入', 12345),
(3, '服装支出', 1000),
(4, '吃饭支出', 325),
(5, '股票收入', 8000),
(6, '打麻将支出', 8000),
(7, NULL, 5000)
// 2. 查询所有列
select * from accounts
// 3. 查询 name, money 列
select name, money from accounts
// 4. 查询 name 列并去掉重复的数据
select distinct name from accounts
// 5. 查询 name 列并重新命名(重新命名为临时改名, 不会真的改数据库里的列名)
select name as 'myname' from accounts
// 6. 将查询结果做数学运算
select name, money + 100 from accounts
// 7. 查询所有的吃饭支出
select * from accounts where name = '吃饭支出'
// 8. 查询所有金额大于 1000 的数据
select * from accounts where money > 1000
// 9. 查询金额大于等于 2000 且小于等于 5000 的数据
select * from accounts where money >= 2000 and money <= 5000
select * from accounts where money between 2000 and 5000
// 10. 查询金额是 1000, 3000, 8000 其中的
select * from accounts where money = 1000 or money = 3000 or money = 8000
select * from accounts where money in (1000, 3000, 8000)
// 11. 查询所有 name 带支出字眼的数据
select * from accounts where name like '%支出%'
// 12. 查询 name 是 5 个字的
select * from accounts where name like '_____'
// 13. 查询 name 不为空的
select * from accounts where name is not null
select * from accounts where not (name is null)
// 14. 对 money 进行升序排序(升序是asc, 默认是升序, 可以不写)
select * from accounts order by money
// 15. 对 money 进行降序排列
select * from accounts order by money desc
// 16. 查询出所有 name 有支出字眼的数据, 并按照金额降序排序
select * from accounts where name like '%支出%' order by money desc
聚合函数
// 1. 查询表中总共有多少条数据
select count(*) from accounts
// 2. 查询表中 money 列的总和
select sum(money) from accounts
// 3. 查询表中 money 列 name 包含字眼 的条件求和
select sum(money) from accounts where name like '%支出%'
// 4. 查询表中 money 列的最大值、最小值
select max(money) from accounts
select min(money) from accounts
// 5. 查询表中 money 列的平均值
select avg(money) from accounts
分组统计(必须跟随聚合函数)
// 1.
select sum(money), name from accounts group by name
select sum(money), count(money), money from accounts group by money
// 2. 对 name 进行分组查询求和, 但是只要 name 有支出字眼的, 并降序排列
select sum(money) as sum, name from accounts where name like '%支出%' group by name order by desc
// 3. 对 name 进行分组查询求和, 但是只要 name 有支出字眼的, 且金额大于 1000 的
select sum(money) as sum, name from accounts where name like '%支出%' group by name having sum > 1000
外键
create table users (
sid int,
score int,
constraint users_to_fk foreign key (sid) references stu (id)
);
alter table users add constraint users_to_fk foreign key (sid) references stu (id);
多对多的创建关系表