当前位置:网站首页>Basic operation of database
Basic operation of database
2022-07-01 14:42:00 【It is small new】
Change table name
alter table Table name rename The new name of the table ;
alter table student rename student_info;
Change field name , type
alter table Table name change Name New column names data type ;
alter table student_info change student_name student_name int;
Add fields
alter table Table name add Column name type ;
alter table student_info add student_age int;
Delete field
alter table Table name drop Name ;
Change field type ( Try not to change )
alter table Table name modify Name New data types ;
Modify database character set
alter database Database name character set utf8
alter database _111_ character set utf8
Be careful : Need to restart mysql service , To take effect
Modify table character set
Alter table Table name character set utf8 collate utf8_general_ci
use student_info;
-- Change table name
alter table student rename student_01;
-- Change field name
alter table student_01 change name student_age int(11);
-- Change the field name and data type
alter table student_01 change student_age student_name VARCHAR(255);
-- Add fields
alter table student_01 add student_age int(11);
-- Delete field
alter table student_01 drop student_name;
-- Change field type
alter table student_01 modify student_age VARCHAR(255);
-- Modify database character set
alter DATABASE student_info CHARACTER set utf8
-- Modify table character set
Alter table student_01 character set utf8 collate utf8_general_ci
Constraint classification
Primary key primary key
Foreign keys foreign key
only unique
Non empty not null
Self increasing auto_increment
Default default
-- Primary key constraint
--1 Create table time Add primary key
create table person(
id int,
name varchar(255),
income decimal(18,2),
primary key(id)
);
-- Uniqueness
alter table person add unique(id);
--2 Create the table first Then add the primary key
create table person_01(
id int,
name varchar(255),
income decimal(18,2)
);
alter table person_01 add primary key(id);
conditional
and
And , and , It means , Commonly used in Two additional judgments must be met , Equate to java Medium &&
grammar :
select Column qualification from Table limit where A expression and B expression ;
Such as : Query the student table ,name It's Zhang San and the score is greater than 90 branch
select * from student where name=' Zhang San ' and score > 90;
Only students who meet two conditions will be found
or
Or meaning , Commonly used in In the case of an added judgment , Equate to java Medium ||
grammar :
select Column qualification from Table limit where A expression or B expression ;
Such as : Query the student table ,name It's Zhang San or Results are greater than 90 branch
select * from student where name=' Zhang San ' or score > 90;
As long as any one of the two conditions is met , Can
and Priority ratio or high
between .. and ..
stay ... Between
grammar
select Column qualification from Table limit where Name between value 1 and value 2;
in
In the specified data
grammar
select Column qualification from Table limit where Name in( value 1, value 2....);
like
Fuzzy query
grammar :
select Column qualification from Table limit where Name like ' value ' ;
order by Sort
grammar :
select Column qualification from Table limit order by Name asc/desc;
Asc : Ascending
Desc : Descending
limit Limit
grammar
select Column qualification from Table limit limit Number of pieces ;
select Column qualification from Table limit limit Starting value ( It doesn't contain ) , Number of pieces ;
Single table query
Group keywords group by
Common group functions
count(*) : Total number of articles
max( Field name ) : Maximum
min( Field name ) : minimum value
avg( Field name ) : Average
sum( Field name ) : The sum of the
create table student (
id int ,
name varchar(20),
teacher_id int,
score decimal(18,2) ,
primary key (id)
);
create table teacher(
id int ,
name varchar(20),
primary key (id)
);
insert into teacher (id,name)values(1,' Teacher zhang ');
insert into teacher (id,name)values(2,' Teacher wang ');
insert into student (id,name,teacher_id,score)values(1,' Zhang San ',1,90);
insert into student (id,name,teacher_id,score)values(2,' Li Si ',2,88.9);
insert into student (id,name,teacher_id,score)values(3,' Wang Wu ',1,45.7);
insert into student (id,name,teacher_id,score)values(4,' Zhao Liu ',1,84);
insert into student (id,name,teacher_id,score)values(5,' Xiao Ming ',2,92.5);
insert into student (id,name,teacher_id,score)values(6,' Xiaohong ',2,47);
grammar :
select count(*),max( Field name ),min( Field name )... from Table name group by Field name ;
Such as : See how many students there are in the student list
select count(*) from student;
Such as : Check that the score in the student table is greater than 90 How many students are there
select count(*) from student where score > 90;
Group by
Such as : Query how many students each teacher brought ( Show teacher id that will do )
select teacher_id, count(*) as stu_count from student group by teacher_id;
Such as : Check the highest score of the students brought by each teacher
select teacher_id, count(*) as stu_count,max(score) as stu_max_score from student group by teacher_id;
Such as : Check the total score and average score of each teacher's students
select teacher_id, sum(score) as sum,avg(score) as avg from student group by teacher_id;
Union And union all
Merge query , Merge query results
Union Will remove duplicates
Union all Duplicates will not be removed
Such as : Query out teacher_id = 1 All the student information of
select * from student where teacher_id=1;
Such as : Query out The student's score is greater than 60 All the student information of
select * from student where score > 60;
Such as : Query out The student's score is greater than 60 or teacher_id = 1 All the student information of ( Remove duplication )
// use or Realization
select * from student where teacher_id=1 or score > 60;
// use union Realization
select * from student where teacher_id=1
union
select * from student where score > 60;
Such as : Query out The student's score is greater than 60 or teacher_id = 1 All the student information of ( repeatable )
select * from student where teacher_id=1
union all
select * from student where score > 60;
Common functions
-- Show current version
select version();
-- Display the current database
select database();
-- Return the number of characters
select char_length(' After dinner ');
-- Returns the number of bytes occupied
select length(' War Within Three Kingdoms ');
-- String concatenation function return abc
select concat('a','b','c');
-- String concatenation function The first is the splice return a+b+c
select concat_ws('+','a','b','c');
-- Capitalize all letters
select upper('abcd');
-- Turn all letters to lowercase
select lower('ABCD');
-- Number of characters intercepted 1 Indicates which character to start with 3 Indicates how many are intercepted
select substring(' System information class ',1,3);
边栏推荐
- That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!
- 2022-2-15 learning the imitation Niuke project - Section 3 post details
- [Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
- Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
- Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
- MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
- tensorflow2-savedmodel convert to pb(frozen_graph)
- Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界
- Use of Oracle database objects
- Rearrangement of overloaded operators
猜你喜欢
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
户外LED显示屏应该考虑哪些问题?
微服务大行其道的今天,Service Mesh是怎样一种存在?
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
[15. Interval consolidation]
Opencv interpolation mode
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
sqlilabs less13
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障
随机推荐
tensorflow2-savedmodel convert to tflite
tensorflow2-savedmodel convert to tflite
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
Is the futures company found on Baidu safe? How do futures companies determine the regularity
Pat 1065 a+b and C (64bit) (20 points) (16 points)
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
Basis of target detection (NMS)
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
Halo effect - who says that those with light on their heads are heroes
C 语言进阶
2022-2-15 learning xiangniuke project - Section 4 business management
Research Report on the development trend and competitive strategy of the global high temperature label industry
JVM performance tuning and practical basic theory part II
Details of appium key knowledge
Research Report on the development trend and competitive strategy of the global display filter industry
深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障
SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT
如何看待国企纷纷卸载微软Office改用金山WPS?
How to pass array parameters in get request