MYSQL的增删改查

MYSQL的增删改查做题笔记

DESC employee
USE hsp_db03
CREATE TABLE good
(id INT,
name VARCHAR(255),
price DOUBLE
);
INSERT INTO good (id,name,price) VALUES (21,'华为',3000.0);

SHOW TABLES
-- 显示表的结构,可以查看表的所有列
DESC good
SELECT * FROM good;
-- 修改表中的全部信息
UPDATE good SET price=3500
-- 修改某一行的数据
UPDATE good SET price=5000 where id=21
-- DELETE 整个表的内容
DELETE FROM good
-- 删除某一行
DELETE FROM good WHERE id=20
-- 使用delete智能删除记录,不删除表本身,如果要删除表只能drop 表名

-- 修改表名
ALTER TABLE good RENAME to goods

SELECT * FROM goods
-- 往表中插入一列
ALTER TABLE goods ADD nums INT NOT NULL DEFAULT 100 AFTER price
-- 将nums列的数据类型改为 VARCHAR
ALTER TABLE goods MODIFY nums VARCHAR(10) NOT NULL DEFAULT '100'
-- 删除nums列
ALTER TABLE goods DROP nums
-- 修改字符集
ALTER TABLE goods CHARACTER SET utf8
-- 修改列名
ALTER TABLE goods CHANGE name good_name VARCHAR(255) NOT NULL DEFAULT ''

-- DISTINCT 去除重复数据

create table student
(
id int not null default 1,
name varchar(255) not null default '',
chinese float not null default 0.0,
english float not null default 0.0,
math float not null default 0.0
);
insert into student values(1,'韩顺平',89,78,90);
insert into student values(2,'藏非',25,88,97);
insert into student values(3,'关于',88,78,94);
insert into student values(4,'鲁北',68,58,97);
insert into student values(5,'赵云',85,68,95);
insert into student values(6,'马超',59,28,92);
insert into student values(7,'宫本',79,18,80);
select * from student;
-- 要查询的记录,每个字段都相同才会去重
select distinct name,english from student;

select name,(chinese+english+math) from student;

select name,(chinese+english+math)as total_score from student;

select name,(chinese+english+math) from student where (chinese+english+math)>200;

select * from student where math>chinese
-- 找出韩字开头的全部学生
select * from student where name LIKE '韩%'

select * from student where math in(90,97)

-- 按照某一列排序
select * from student order by(chinese+english+math) desc
select name id,name,chinese,english,math,(chinese+english+math) as total_score from student order by total_score desc
-- 找出姓韩的总分按照desc排序
select id,name,chinese,english,math,(chinese+english+math) as total_score from student where name like '关%'order by total_score desc
select *,(chinese+english+math) as total_score from student where name like '关%'order by total_score desc
-- count(*) 返回满足条件的记录的行数
-- count() 统计满足条件的某列有多少个,但是会排除null
create table tmp
(name varchar(255));
insert tmp (name) values('Tom');
insert tmp (name) values('Jack');
insert tmp (name) values('Csj');
insert tmp (name) values(null);
-- 会自动的将该列中的null哪一行记录去除
select count(name) from tmp
select count(*) from tmp
select * from tmp
select sum(math) from student
select avg(math) from student
select max(math),min(math) from student



你可能感兴趣的:(MySQL,mysql,数据库)