自学内容网 自学内容网

【快捷入门笔记】mysql基本操作大全-SQL插入、查询、更新、删除

INSERT插入

基本语法

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

*1.列名和值要一一对应
*2.如果表名后不定义列名,则默认给所有列添加值
*3.除了数字类型。其他类型value单、双引号引起

1、插入单行

INSERT INTO 猫 ( id, NAME, age,price ) VALUES ( 1, 'zifu', 2 ,28.26);

2、 插入多行

建表:

CREATE table IF NOT EXISTS fang (
 id int,
 name VARCHAR(20),
 age int,
 sex VARCHAR(5),
 address VARCHAR(100),
 math int,
 english DECIMAL
 );

对fang表插入多行数据

INSERT into fang 
(id,name,age,sex,address,math,english)
VALUES
(1,'ff',34,'女','aadad',21,212),
(2,'ff',24,"男","aadad",21,212),
(3,'ff',14,"女","aadad",21,212);

3、从一个另一个表插入数据

INSERT INTO table1 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table2
WHERE condition;

4、使用默认DEFAULT

建表,如果breed列没有规定。默认都是其他

CREATE TABLE IF NOT EXISTS 猫
(
   id INT PRIMARY KEY,
   NAME VARCHAR ( 50 ) NOT NULL,
   breed VARCHAR ( 50 ) DEFAULT '其他',
   age INT CHECK ( age >= 0 ),
   price DECIMAL ( 10, 2 ) NOT NULL 
   );

插入breed为DEFAULT,此时DEFAULT ‘其他’,结果为 ‘其他’

INSERT INTO 猫 (id, NAME, breed)
VALUES (6, 'Frank White', DEFAULT);

Select查询

语法:
select 字段列表
from 列表名
where 条件列表
GROUP BY 分组字段
HAVING 分组之后的条件
ORDER BY 排序
LIMIT 分页限定

1、基本查询

SELECT name,age FROM fang;

2、去除重复的结果集

select DISTINCT address from fang
select DISTINCT name,address from fang

3、使用select计算

带计算的SELECT 在查询中执行计算

SELECT column1 * column2 AS result FROM table;

例:
计算math English分数之和

select math,english,math+english from fang;

如果有NULL参与的计算 计算结果都为NULL,需要函数IFNULL,
英语成绩为NULL,替换为0

select math,english,math+IFNULL(english,0) from fang;

4、带函数的SELECT

使用内置的SQL函数
在这里UPPER(列名)函数的作用是将列转化为大写

SELECT UPPER(column) FROM table;

5、重命名列

SELECT 在结果集中重命名列 可以用AS AS可以用空格取代

SELECT column AS "New Name" FROM table;

Select插入选择

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

1、基于多个条件插入

INSERT INTO 表名(列名1,列名2,列名3)
SELECT 列名1,列名2,列名3
FROM 旧表名
WHERE 条件1 AND 条件2

INSERT INTO it_team (id, name, age)
SELECT id, name, age
FROM old_employees
WHERE department = 'IT' AND age > 25;

符合条件1或者 条件2的都要

WHERE department = 'IT' OR department = 'HR';

TOP5员工薪资递减排序

INSERT INTO top_earners (id, name, salary)
SELECT TOP 5 id, name, salary
FROM employees
ORDER BY salary DESC;

update更新

update set 更新

UPDATE products
SET price = price * 0.9, discount_applied = TRUE
WHERE last_sold_date < DATE_SUB(CURRENT_DATE, INTERVAL 6 MONTH);

delete删除

1、基本DELETE 基于条件删除行

DELETE FROM students WHERE name = 'John Doe';

2、带有多个条件的DELETE 删除满足多个条件的行

DELETE FROM students WHERE age > 30 AND gpa < 2.5;

3、带有IN子句的DELETE 基于值列表删除行

DELETE FROM students WHERE city IN ('New York', 'Los Angeles', 'Chicago');

4、删除所有记录 从表中删除所有行

DELETE FROM students;

5、带有JOIN的DELETE 从多个表中删除相关记录

DELETE students, enrollments FROM students JOIN enrollments ON students.id = enrollments.student_id WHERE students.fees_paid = 0;

6、带有子查询的DELETE 基于另一个查询的结果删除

DELETE FROM enrollments WHERE student_id IN (SELECT id FROM students WHERE graduation_year < 2020);

原文地址:https://blog.csdn.net/qq_44871101/article/details/143711579

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!