当前位置:网站首页>MySQL basic learning

MySQL basic learning

2022-07-26 05:07:00 nineteen million nine hundred and twenty-one thousand one hundr

0、MySQL Database introduction

0.1、 Common types of databases

 Relational type :  Have a watch 、 Have a library , To store and express the relationship between data .
    oracle、mysql、sql server、sqlite
⾮ Relational type :  Store according to key value , No table 
    Redis、Mongodb

0.2、sql Language classification

0.3、 Commonly used sql sentence

 Fuzzy query 
     arbitrarily ⼀ Characters :_
		 Search for two word names ⽣ select * from students where name like '__'
	 Any number of characters :%
		 Query the surname Bai and age ⼤ On 20 Science ⽣ select * from students where name like ' hundred %' and age>20

 Continuous range  between  Start  and  end 
    select * from students where age not between 20 and 25

 Inquire about 1 Except class, other classes learn ⽣ The average age of 、 most ⼤ Age 、 most ⼩ Age 
	select class, avg(age),max(age),min(age) from students group by class having
class != "1 class "

where and having The difference between ?
	where It's right from Data filtering is performed on the specified table , It belongs to the screening of original data .
	having It's right group by Results of .having In the following conditions, you can use the aggregate function ,where You can't .

 Pagination 
	 Computation first ⼏⻚ Get data formula : 
		select * from  Table name  limit (n-1)*m,m n: The first ⼏⻚ m: Every time ⻚ Show multiple 

 Internal connection another ⼀ Species writing 
    select * from students,scores where students.studentNo=scores.studentno
     Conclusion : production ⽣ Cartesian product , Of board ⾏ Speed will ⾮ Often slow . Forbid ⽌

⼦ Inquire about 
    sql Nested in statement sql sentence 
    ⼦sql Statements either serve as conditions , want ⻔ Act as a data source 
    select * from scores where studentNo = (select studentNo from students where
name = ' Wang Zhaojun ’)
     Inquire about 18 Years old school ⽣ The achievement of , Ask to show the result ( Column ⼦ Inquire about )                                        
    select * from scores where studentNo in (select studentNo from students where
age=18)
     I'm in the same class as Wang Zhaojun 、 Study at the same age ⽣ Information (⾏⼦ Inquire about )                                        
	select * from students where (class,age)=(select class,age from students where
name=' Wang Zhaojun ')
     Act as a data source 
    select * from scores inner join (select * from courses where name in (" data 
 library "," The system test ")) temp on temp.courseNo=scores.courseNo  
     List each department ⻔ in ⼯ information ⼩ In this department ⻔ Average ⼯ Endowment member ⼯ Information 
     select e.empname  full name ,s.salary ⼯ information ,d.deptname  Ministry ⻔, temp.avg_salary  Ministry ⻔ Average salary  
    from employees e
    inner join departments d
    on e.deptid=d.deptid
    INNER JOIN salary s
    on s.empid=e.empid
    inner join (select d.deptid,avg(s.salary) avg_salary from employees e inner
    join departments d on e.deptid=d.deptid INNER JOIN salary s on
    s.empid=e.empid group by d.deptname) temp
    on temp.deptid=d.deptid
    where s.salary< temp.avg_salary    

 1、 Common commands

 Login related 
    mysql -u root -p   Log in locally MySQL
    mysql -h 182.167.12.3 -u user1 -p -P 3308  Appoint IP Address and port number login MySQL database 

 Delete 
    delete from  Table name  where  Field = value ; Delete ⼀ Records or partial records 
    truncate table  Table name ;  Delete all records ( Truncation table )
    drop table  Table name  ;  Delete table 

delete  And  truncate The difference between ?
    delete from  Table name 
         explain : Primary key ⾃ increase ⻓ Not from 1 Start , You can make ⽤where Conditions 
    truncate table  Table name 
         explain : Retention structure , Primary key ⾃ increase ⻓ from 1 Start , Don't make ⽤where Conditions , Speed ⽐delete fast 


 keyword 
    show databases,   See which libraries are currently available 
    use  Database name ;  Choose to make ⽤ library ;
    create database  Library name  charset=utf8;⾃⼰ newly build ⼀ Databases 
    drop database  Library name ; Delete database ; 
    select database(); Confirm which library is currently selected 
    show tables,   View all tables under the current library 
    desc  Table name ,   Display table design 
    show create table  Table name ,   Show the table creation statements, including indexes 
      

 Data constraints 
    1、 Primary key (primary key): Only ⼀ identifier 
    2、⾮ empty (not null): This field is not allowed to fill in null value 
    3、 But ⼀(unique): Duplicate values are not allowed for this field 
    4、 The default value is (default): When this value is not filled in, it will make ⽤ The default value is , If it is filled in, the filling shall prevail 
    5、 Foreign keys (foreign key): Maintain the association between two tables 

2、 Data type and corresponding length

2.1、 Integers

         The integer type is a numeric value without a decimal part , You can set signed and unsigned types ( It's a positive number )

 2.2、 decimal

        MySQL Floating point numbers and fixed-point numbers are used in to represent decimals .
         There are two floating point types , They are single precision floating-point numbers (FLOAT) And double precision floating point numbers (DOUBLE); There is only one fixed-point type , Namely DECIMAL.
         Both floating-point and fixed-point types can be used (M, D) To express , among M It's called precision , The total number of digits ;D Is called a scale , Represents the number of decimal places .
         The value range of floating-point number type is M(1~255) and D(1~30, And can't be greater than M-2), Indicates the display width and decimal places respectively .M and D stay FLOAT and DOUBLE Is optional ,FLOAT and DOUBLE The type will be saved to the maximum precision supported by the hardware .DECIMAL Default D The value is 0、M The value is 10.

        FLOAT The value range of type is as follows :
         Signed value range :-3.402823466E+38~-1.175494351E-38.
         The range of unsigned values :0 and -1.175494351E-38~-3.402823466E+38.

        DOUBLE The value range of type is as follows :
         Signed value range :-1.7976931348623157E+308~-2.2250738585072014E-308.
         The range of unsigned values :0 and -2.2250738585072014E-308~-1.7976931348623157E+308.
         The advantage of floating-point numbers over fixed-point numbers is that when the length is fixed , Floating point numbers can represent a larger range ; The disadvantage is that it will cause accuracy problems .

         Last but not least : stay MySQL in , The number of fixed points is stored as a string , When the accuracy requirements are relatively high ( Such as currency 、 Scientific data ), Use DECIMAL Type is better , The other two floating-point numbers are also prone to problems in subtraction and comparison , So when you use floating-point numbers, you need to pay attention to , And try to avoid floating-point comparison .

 2.3、 The date type   

        MySQL There are several data types representing dates in :YEAR、TIME、DATE、DTAETIME、TIMESTAMP. When only year information is recorded , You can just use YEAR type .

 2.4、 String type

        MySQL The string types in are CHAR、VARCHAR、TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT、ENUM、SET etc. .VARCHAR(M) Is a variable length string ,M Indicates the length of the largest column ,M The range is 0~65535. The length limit cannot exceed 65535 Bytes . According to character set , If the character type is gbk, Each character occupies 2 Bytes , The maximum length cannot exceed 65535/2 =32766;   If the character type is utf8, Each character takes up a maximum of 3 Bytes , The maximum length cannot exceed 65535/3 =21845

 2.5、TEXT  type

         TEXT Column holds non binary strings , Such as the content of the article 、 Comments, etc . When saving or querying TEXT Column value , Do not delete trailing spaces .TEXT Types are divided into 4 Kind of :TINYTEXT、TEXT、MEDIUMTEXT and LONGTEXT. Different TEXT The storage space and data length of types are different .

        TINYTEXT The length is 255(28-1) Character TEXT Column .

        TEXT The length is 65535(216-1) Character TEXT Column . Note that this unit is a character , If the character is GBK Then for varchar Of 2 Times the length ,UTF-8 be 3 Times the length

MEDIUMTEXT The length is 16777215(224-1) Character TEXT Column .
LONGTEXT The length is 4294967295 or 4GB(232-1) Character TEXT Column .

 2.6、ENUM type

        ENUM Values are internally expressed as integers , Each enumeration value has an index value ; The member values allowed by the list value are from 1 Numbered starting ,MySQL This index number is stored , Enumerations can have at most 65535 Elements .

 2.7、 Binary string type

         Binary string types are sometimes referred to directly as “ Binary type ”.MySQL Binary strings in are BIT、BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB and LONGBLOB.

 2.8、BLOB type

         BLOB It's a binary object , Used to store a variable amount of data .

原网站

版权声明
本文为[nineteen million nine hundred and twenty-one thousand one hundr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260500339308.html