[MySQL] 5 minutes to learn basic database operations (1)

(The full text is about 5500 words, and the expected reading time is 5 minutes)


1. What is SQL?

structured Query Language: structured query language

classification:

1) DDL (Data Definition Language) data definition language is
used to define database objects: databases, tables, columns, etc. Keywords: create, drop, alter, etc.
2) DML (Data Manipulation Language) data manipulation language is
used to add, delete, and modify data in the tables in the database. Keywords: insert, delete, update, etc.

3) DQL (Data Query Language) data query language is
used to query the records (data) of the table in the database. Keywords: select, where, etc.
4) DCL (Data Control Language) data control language (understand) is
used to define database access rights and security levels, and create users. Keywords: GRANT, REVOKE, etc.

2. About database CRUD operations

#Create
create database hzyc;
create database if not exists hzyc98 character set gbk;
#Retrieve
show databases;
show create database hzyc98;
#Update
alter database hzyc98 character set gbk;
#Delete
drop database hzyc98;
drop database if exists hzyc98; 
#查看当前使用的数据库
select database();
show tables;
use hzyc98

1. Operation list:

The table name/header is: zoomlist

#查
show tables; -- show tables_in_hzyc98
desc zoomlist;

#增
create table zoomlist (
	Name  varchar(30),
	Age	  int,
	ID	  int,
	Height double(5,1)
)

#删
drop table if exists zoomlist;

#改
alter table zoomlist rename to newzoomlist;
alter table zoomlist character set gbk;
alter table zoomlist add Name varchar(20);#加列
alter table zoomlist change Age newAge int;
alter table zoomlist modify Age char(8);
alter table zoomlist drop Name;

/*设置类型:*/
 - intdouble(5,1)varchar(20) 
 - date 	#yyyy-MM-dd
 - datetime #yyyy-MM-dd HH:mm:ss 
 - timestamp#时间戳 yyyy-MM-dd HH:mm:ss

2. Operate the data in the table:

#除了数字,其他都需要引号来赋值
insert into zoomlist (Name, Age, ID, Height) value('美洲豹',5,'20201207',3.2);
insert into zoomlist ('美洲豹',5,'20201207',3.2);

#删除
delete from zoomlist where [条件];
delete from zoomlist;
TRUNCATE TABLE zoomlist;

#修改
update zoomlist set Name = '大笨象' Age = 12 where address = '深圳';
update zoomlist set address = '深圳';

a. Query

#查询
#尽量不要用 * 先desc一下表里面有啥,然后在决定展示什么东西。
SELECT * FROM zoomlist; 
SELECT Name,Age FROM zoomlist;	 --只显示某个列,方便查看!
SELECT DISTINCT Name FROM zoomlist; --去除结果中[完全重复]的

SELECT Name,score1,score2,scroe1+scroe2 FROM zoomlist;--as:自定义名字展示,也可以不写as
SELECT Name,scroe1+IFNULL(scroe2,0) 总分 FROM zoomlist; --ifnull遇到没有值的直接给赋值为0
SELECT Name,score1,score2,scroe1+IFNULL(scroe2,0) AS 总分 --显示表头
FROM zoomlist,peoplelist; --从zoomlist、peoplelist里面获取

b.where conditions:

* ><<=>==!=<>--不等号
* andornot --关键字比&&、||、!好用推荐
* BETWEEN...AND --范围内都符合就行
* IN( 集合) --特定值的范围
* LIKE:模糊查询(1)_:单个任意字符;(2%:多个任意字符
* IS NULL

例子:
select Name, Age from Student where age between 12 and 20;
select Name, Age from Student where age in (12,14,16,18);
select Name, Age from Student where name like '%牛%'; --查名字里面包含了牛的学生
select Name, Age from Student where name is not null; -- 查询学生:名字空的不查

Three, query

1. Sort query

select * from employee order by age;
select * from employee order by age asc; --升序
select * from employee order by age desc; --降序
select * from employee order by age desc height desc; --第一个一样的时候,才会用第二个方法排序(age降序,身高降序)

2. Aggregate functions (calculation of columns)

Excluding null data, and null data will not participate in the calculation, no error will be reported!

  1. count: count the number
  2. min, max, sum, avg: evaluation
select count(*) from student;
select count(ifnull(age,20)) from student; 
select count(age) from student;--如果没有就不记录
select count(id) from student; --我们一般选用主键来统计个数

select max(age) from student;
select min(age) from student;
select sum(age) from student;
select avg(age) from student;

3. Group query

After group by are two different groups, they can no longer view an independent individual.

  • Fields to be queried after grouping: grouping fields, aggregate functions.
  • The difference between where and having?
    • Where is defined before grouping, and having is defined after grouping;
    • Where does not meet the conditions do not participate in the grouping, having does not meet the conditions will not be displayed;
    • Only having can be followed by aggregate function judgment.
select sex,count(name) from employee group by sex having count(name)<6;

select sex,count(name) from employee where name = '张四' group by sex ;

4. Sort query

limit is a dialect of MySQL, used for paging

SELECT * FROM student LIMIT 0,5; -- 第1页,从0索引开始,读5个数据

SELECT * FROM student LIMIT 7,10; -- 第2页,从7索引开始(第8个数据),读10个数据

Four, constraints

  • constraint:
    1. Primary key constraint: primary key
    2. Non-empty constraint: not null
    3. The only constraint: unique
    4. Foreign key constraint: foreign key

1. Non-empty constraint: not null

-- 建表时添加非空约束:
 create table employee(
 	name char(30),
 	sex char(8) not null
 )
 alter table employee modify sex char(8) not null; --添加非空约束
 alter table employee modify sex char(8); --破除非空约束
 

Non-empty constraint instance

2. Unique constraint

There can only be one null value, no more;
delete constraints can only use drop index to delete unique constraints

-- 建表时添加唯一约束:
 create table employee(
 	name char(30),
 	sex char(8),
 	score int unique --分数要唯一
 )
 
--添加唯一约束
alter table employee modify name char(8) unique;
 
--破除唯一约束
-- alter table employee modify sex char(8); 不可用
--破除name身上的unique约束用drop index除去索引
alter table employee drop index name; 

Example operation:

The unique constraint cannot be modified directly
drop deletes the unique constraint successfully

3. Primary key constraint: primary key

A table has only one primary key, which is non-empty and unique
as the unique identification of records, equivalent to index

-- 建表时添加主键约束:
 create table employee(
 	id int  primary key, --给id加上主键约束
 	name char(30),
 )
 
--添加唯一约束
alter table employee modify id int primary key;
 
--破除唯一约束
-- alter table employee modify id int; 不可用!
--破除id身上的primary key约束只能用drop primary key
alter table employee drop primary key; 

4. Automatic growth: auto_increment

Only useful for values, and generally can be placed on the primary key for automatic growth

-- 建表时添加auto_increment:
 create table employee(
 	id int auto_increment, --给id加上auto_increment
 	name char(30),
 )
 
--添加auto_increment,自动从1开始
alter table employee modify id int auto_increment;
--设置初值
alter table employee auto_increment = 100;
 
--破除auto_increment
alter table employee modify id int; 

Five, summary

We learned what SQL is, made a simple introduction, and also listed some basic MySQL operations, as well as queries and constraints.

But I have just been exposed to MySQL, so the basic operators are relatively new, and I need to learn more and practice more to get the true knowledge.

In the future, we will also learn about MySQL's multiple relationships, multi-table queries, transactions (not sure what they are), JDBC statements, database connection pool druid, JDBCTemplate... There are many things to learn, but the task is still It lies in sorting out the basic things and consolidating the foundation is the last word! ! !

Error example:

If you do not write column names when adding data, you must give all column values, otherwise an error will be reported!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43801418/article/details/110820203