当前位置:网站首页>Basic MySQL database operations

Basic MySQL database operations

2022-06-22 22:34:00 Fire eye Dragon

1、DDL Database operation of operation

--  View all databases 
show DATABASES;
--  Create database 
CREATE DATABASE [IF not EXISTS] mydb1 [CHARSET=UTF8];
--  Choose to use the database 
use mydb1;
--  Delete database 
drop DATABASE[ if EXISTS] mydb1;
--  Modify database code 
alter DATABASE mydb1 CHARACTER set utf8;

notes :1. Things in brackets can be omitted , You need to remove the brackets before copying
2.IF not EXISTS(if EXISTS) The difference between adding and not adding is that there is no error when adding , If not, if the database exists ( non-existent ) May be an error .

2、DDL Operation table creation

--  choice mydb1
use mydb1;
--  Create table 
CREATE TABLE IF NOT EXISTS student(
sid int,
name varchar(20),
gender varchar(1),
age int,
birth date,
address varchar(20),
score double
);

data type :
Data type refers to specifying the data type for the fields in the table when creating the table , Only data that meets the type requirements can be stored , The principle of using data types is : Enough is enough. , Try to use a small range of , No big ones , To save storage space .

value type :

type size Range ( A signed ) Range ( Unsigned ) purpose
TINYINT1 byte(-128,127)(0,255) Small integer value
SMALLINT2 bytes(-32768,32767)(0,65535) Large integer value
MEDIUMINT3 bytes(-8388608,8388607)(0,16777215) Large integer value
INT or INTEGER4 bytes(-2147483648,2147483647)(0,4294967295) Large integer value
BIGINT8 bytes(-9223372036854775808,9223372036854775087)(0,18446744073709551615) Maximum integer value
FLOAT4 bytes(-3.402823466E+38,3.402823466351E+38)0,(1.175494351E-38,3.402823466E+38) Single precision floating point values
DOUBLE8 bytes(-1.7976931348623157E+308,1.7976931348623157E+308)0,(2.2250738585072014E-308,1.7976931348623157E+308) Double precision floating point value
DECIMAL Depend on M and D Value Depend on M and D Value Small value

String type :

type size purpose
CHAR0-255 bytes Fixed length string
VARCHAR0-65535 bytes Variable length string
TINYBLOB0-255 bytes No more than 255 Binary string of characters
TINYTEXT0-255 bytes Text string
BLOB0-65535 bytes Long text data in binary form
TEXT0-65535 bytes Long text data
MEDIUMBLOB0-16777215 bytes Medium length text data in binary form
MEDIUMTEXT0-16777215 bytes Medium length text data
LONGBLOB0-4294967295 bytes Maximum text data in binary form
LONGTEXT0-4294967295 bytes Large text data

The date type :

type size Range Format purpose
DATE3 bytes1000-01-01/9999-12-31YYYY-MM-DD Date value
TIME3 bytes‘-838:59:59’/‘838:59:59’HH:MM:SS Time value or duration
YEAR1 byte1907/2155YYYY The year is worth
DATETIME8 bytes1000-01-01 00:00:00/9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS Mix date and time values
TIMESTAMP4 bytes1970-01-01 00:00:00/2038 The end time is the 2147483647 second , Beijing time. 2038-1-19-11:14:07, GMT 2038 year 1 month 19 Japan In the morning 03:14:07YYYYMMDD HHMMSS Mix date and time values , Time stamp

3、DDL Table of operations other operations

 --  View all tables in the current database 
SHOW TABLES;
--  View the creation statement of the specified table 
SHOW CREATE TABLE student;
--  View table structure 
DESC student;
--  Delete table 
DROP TABLE student;

4、 Modify table structure

--  Add columns to :alter table  Table name  add  listed   type ( length )【 constraint 】;
ALTER TABLE student ADD dept VARCHAR(20);
--  Change column name and type :
-- alter table  Table name  change  Old column names   New column names   type ( length ) 【 constraint 】;
ALTER TABLE student CHANGE dept department VARCHAR(30);
--  Modify table delete column :
-- alter table  Table name  drop  Name ;
ALTER TABLE student drop department;
--  Modify the name of the table :
-- rename table  Table name  to  The new name of the table ;
RENAME TABLE student to stu;
  • DML Data insertion for operation
--  Format 1:insert into  surface ( Name 1, Name 2……)values( value 1, value 2……);
--  Give one row of data at a time 
INSERT INTO stu(sid,name,gender,age,birth,address,score) 
VALUES(1001,' Zhang San ',' male ',18,'2001-12-23',' Beijing ',85.5);
--  Give multiple rows of data at a time 
INSERT INTO stu(sid,name,gender,age,birth,address,score) 
VALUES(1002,' Li Si ',' male ',18,'2001-10-23',' nanjing ',70.0),
(1003,' Wang Wu ',' Woman ',17,'2000-12-23',' Shenzhen ',88.5);
--  Assign values to only one or more values 
INSERT INTO stu(sid) values(1004);
INSERT INTO stu(sid,name) values(1005,' Zhao Liu ');

--  Format 2:insert into  surface values( value 1, value 2……);
INSERT INTO stu VALUES(1006,' Huang San ',' male ',18,'2001-5-23',' Beijing ',99.5);

notes : Format 1 The case where multiple lines are given at one time in the format also applies 2

  • DML Data modification of operation
--  Data modification 
--  Format 1:update  Table name  set  Field name  =  value , Field name = value ……;
--  Format 2:update  Table name  set  Field name = value , Field name = value ……where  Conditions ;
UPDATE stu set address = ' Chongqing ';
UPDATE stu set address = ' Beijing ' WHERE sid = 1004;
UPDATE stu set address = ' Shanghai ' WHERE sid >1004;

UPDATE stu set address = ' Beijing ',score = 100 WHERE sid = 1005;
  • DML Data deletion of operation
--  Data deletion 
--  Format :delete from  Table name  [where  Conditions ];
-- truncate table  Table name   perhaps  truncate  Table name 
DELETE FROM stu WHERE sid = 1004;
--  Delete all data in the table 
DELETE FROM stu;
--  Clear table data 
TRUNCATE TABLE stu;

notes :delete and truncate The principle is different ,delete Only delete content , and truncate Be similar to drop table , It can be understood as deleting the whole table , Then create the table .

5、MySQL database DML Operation practice

MySQL database DML Operational exercises

原网站

版权声明
本文为[Fire eye Dragon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221824155642.html

随机推荐