Some notes for MySQL beginners

		             			**MySQL学习过程的一点小笔记**
	最近一段时间作为一个学生,我开始了对MySQL的学习,一下就是我学习过程中的一些笔记,欢迎大佬过来指正,我接下来也会继续自己笔记的上传。在这里谢谢大家的浏览。

– Find the table
SELECT * FROM goods_car;
– primary key, cannot be empty and unique
– auto_increment The data in this column is self-growing
CREATE TABLE goods_car(
id INT(4) PRIMARY KEY AUTO_INCREMENT,
uid INT(10),
good_id INT(10) ,
nums INT(10),
STATUS TINYINT(4),
creat_time INT(10),
update_time INT(10)
);
– delete the database
DROP TABLE test.people;

– Create a primary key independently
– When the table has been generated, then add a primary key
CREATE TABLE people(
people_id INT(4),
people_name VARCHAR(10)
);
– Add a primary key
ALTER TABLE people ADD CONSTRAINT people_pk PRIMARY KEY (people_id);
– No Empty constraint
DROP TABLE people;

CREATE TABLE people(
people_id INT(4) PRIMARY KEY AUTO_INCREMENT,
people_name VARCHAR(10) NOT NULL
);
– 默认约束
DROP TABLE people;

-If the default default value is empty, then use the default value
CREATE TABLE people(
people_id INT(4) PRIMARY KEY,
people_name VARCHAR(10) NOT NULL
DEFAULT'zhangsan ', people_age INT(4) NOT NULL DEFAULT '22'
);

– The only constraint unique
– otherwise it is empty or the value must be different
DROP TABLE people;

CREATE TABLE people(
people_id INT(4) PRIMARY KEY AUTO_INCREMENT,
people_name VARCHAR(10) UNIQUE
);

– 外键约束
DROP TABLE country;
– 父表
CREATE TABLE country(
country_id INT(4) PRIMARY KEY AUTO_INCREMENT,
country_name VARCHAR(10) UNIQUE
);
– 子表
CREATE TABLE people(
people_id INT(4) PRIMARY KEY AUTO_INCREMENT,
people_name VARCHAR(10) UNIQUE,
country_id INT(4),
– 建立外键约束
CONSTRAINT pk_people FOREIGN KEY (country_id) REFERENCES country(country_id)
);
– 基本查询
SELECT * FROM people,country;

– Add column operation
CREATE TABLE people(
people_id INT(4) PRIMARY KEY AUTO_INCREMENT,
people_name VARCHAR(10) UNIQUE
);
– ALTER TABLE table name ADD column name data type operation
ALTER TABLE people ADD people_age INT(4) NOT NULL;

– Delete column operation
– ALTER TABLE table name DROP COLUMN column name
ALTER TABLE people DROP COLUMN people_age;

- modify the data type of the column
- ALTER TABLE table MODIFY COLUMN Column Name Data Type
ALTER TABLE people MODIFY COLUMN people_age VARCHAR ( 10);

– Create two tables country.people
CREATE TABLE country(
country_id INT(4),
country_name VARCHAR(10) NOT NULL
);
CREATE TABLE people(
people_id INT(4),
people_name VARCHAR(10) NOT NULL,
country_id INT(4) NOT NULL
);
– Add constraint
– ALTER TABLE table name ADD CONSTRAINT Constraint name constraint type
– Add primary key constraint
ALTER TABLE people ADD CONSTRAINT pk_people PRIMARY KEY(people_id);
ALTER TABLE country ADD CONSTRAINT pk_country PRIMARY KEY(country_id);

– 添加外键约束
ALTER TABLE people ADD CONSTRAINT fk_people FOREIGN KEY (country_id) REFERENCES country(country_id);

– Delete constraint
– If there is a foreign key, you should delete the child table foreign key before deleting the parent table primary key
– ALTER TABLE table name DROP constraint type [constraint name]
ALTER TABLE people DROP FOREIGN KEY fk_people;
ALTER TABLE country DROP PRIMARY KEY;

– Full query SELECT * FROM table name 1, table name 2;
– query column SELECT column name 1, column name 2 FROM surface 1, table name 2;
SELECT *FROM people, country;
SELECT country_id FROM people;

– Condition query
– SELECT TABLE *|Column name FROM Table name WHRER Condition expression
– = <> <= >= and or not
SELECT * FROM people WHERE people_name='Zhang San' AND country_id=2;

– Sorting query
– SELECT *|Column name FROM Table name ORDER BY Column name DESC|ASC
– DESC is descending order ASC is ascending order
SELECT * FROM people ORDER BY people_name DESC;

– Fuzzy query
– SELECT *|Column name FROM table name WHERE column name LIKE'';
SELECT * FROM people WHERE people_name LIKE'张%';-- Query
SELECT * FROM people WHERE people_name LIKE'张_';- -Search for Zhang's last name and only two characters

CREATE TABLE country(
country_id INT(4) PRIMARY KEY AUTO_INCREMENT,
country_name VARCHAR(10) NOT NULL
);
CREATE TABLE people(
people_id INT(4) PRIMARY KEY AUTO_INCREMENT,
people_name VARCHAR(10) NOT NULL,
people_age INT(4) NOT NULL

);
ALTER TABLE people ADD country_id INT(4) NOT NULL;
ALTER TABLE people ADD CONSTRAINT fk_people FOREIGN KEY (country_id) REFERENCES country(country_id);
SELECT * FROM people WHERE country_id=1;

– 两张表的连接查询
SELECT people_name,people_age,country_name FROM people a JOIN country b ON a.country_id=b.country_id WHERE country_name LIKE ‘中国%’;

– 左外连接
SELECT people_name,people_age,country_name FROM people a LEFT OUTER JOIN country b ON a.country_id=b.country_id WHERE country_name;

– Character function
– ascii function
SELECT ASCII('a'),ASCII('A'),ASCII('#');
– Character to binary, octal, hexadecimal
SELECT BIN('10'),OCT(' 10'),HEX('10');
– Combine two characters
SELECT CONCAT('hello','world');
– Number of characters in bytes
SELECT LENGTH('hello'),LENGTH('高胜祥');
– The length of the character, commonly used Chinese characters
SELECT CHAR_LENGTH("高胜祥");
– Returns the position of the first occurrence of the string
SELECT INSTR('hello','h');
– substring(str,pos,len) returns str from The len character starting at pos position
SELECT SUBSTRING('helloworld',2,5);
– replace(str,from_str,to_str) replace str's from_str with to_str
SELECT REPLACE('helloworld','world','hape');

– Mathematical function
– Find the absolute value
SELECT ABS(-10.1);
– Find the remainder
SELECT MOD(10,4);
– floor(n) returns the largest integer not greater than n
SELECT FLOOR(-2.1),FLOOR(2.1);
– ceiling(n) returns the smallest integer not less than n
SELECT CEILING(2.1), CEILING(-2.1);
– round(n,d) returns the rounded value of n, with d decimal places, d defaults to 0
SELECT ROUND(2.4), ROUND(2.45,1),ROUND(2.1,-1);
– pow(x,y) x to the power of y
SELECT POW(2,4);
– sqrt(n) returns the square root of a non-negative number n
SELECT SQRT(4 );
– rand() returns the value of a random floating-point number from 0 to 1.0.
SELECT RAND();
– truncate(n,d) intercepts the d decimal place of the number n and returns it without rounding
SELECT TRUNCATE(RAND(), 2),TRUNCATE(1.88,1);

– Date function
– Return the current system time in the format of'yyyy-mm-dd hh:mm:ss'
SELECT SYSDATE(),NOW();
– Return the current date in the format of'yyyy-mm-dd'
SELECT CURRENT_DATE() ;
– Year(date) returns the year of date
SELECT YEAR(NOW());
– month(date) returns the month of date
SELECT MONTH(NOW());
– dayofyear(date) returns date is the day of the year
SELECT DAYOFYEAR(NOW());
– dayofmonth(date) returns date is the day of the month
SELECT DAYOFMONTH(NOW());
– weekday(date) dayofweek(date) returns date is the day of the week 0=Monday…6= Sunday
SELECT WEEKDAY(NOW());
– dayofweek(date) returns date is the day of the week 1=Monday…7=Sunday
SELECT DAYOFWEEK(NOW());
– Find the time difference
SELECT DATEDIFF(CURRENT_DATE(),'2018 -09-12');

Goodbye. . .

Guess you like

Origin blog.csdn.net/m0_49708063/article/details/108689902