Entity integrity (primary key constraint unique constraint self-growth)
domain integrity (non-null constraint default value constraint)
referential integrity (foreign key constraint)
Constraint
primary key constraint
Primary key constraint:
The data in the column that is constrained by the primary key constraint cannot be NULL, and cannot be repeated [not empty, unique]
Note: There can only be one primary key constraint
DROP TABLE dept in each table ;
-
When creating the table, add the primary key constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY,-add the primary key constraint
dname VARCHAR(10),
address VARCHAR(100)
) to the deptno field ;
INSERT INTO dept VALUES(20,'Research Department x' ,'Dallas'); – The addition is successful because it does not violate the rules of primary key constraints.
INSERT INTO dept VALUES(20,'Research Department x','Dallas'); – The addition fails because 20 is repeated -
Delete the primary key constraint
ALTER TABLE dept DROP PRIMARY KEY; -
Set the primary key constraint for the created table
ALTER TABLE emp CHANGE empno empno INT(11) PRIMARY KEY; -
Primary key increment
DROP TABLE dept;
CREATE TABLE dept(
deptno INT PRIMARY KEY AUTO_INCREMENT, – Add a primary key constraint to the deptno field
dname VARCHAR(10),
address VARCHAR(100)
); -
0 and null use self-increasing value
INSERT INTO dept VALUES(0,'Research Department x','Dallas');
INSERT INTO dept VALUES(null,'Research Department x','Dallas');
INSERT INTO dept (dname, address) VALUES('Research Department x','Dallas'); -
The specific value uses the specific value
INSERT INTO dept VALUES (999,'Research Department x','Dallas'); the
only constraint
is the uniquely constrained field, the value cannot be repeated.
DROP TABLE dept; -
When creating a table, add a unique constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY,
-add a primary key constraint to the deptno field dname VARCHAR(10) NOT NULL,-add a non-null constraint to the DNAME field,
address VARCHAR(100) UNIQUE NOT NULL-add unique and non-null constraints to address
); -
Delete the unique constraint: The unique constraint is a kind of index, so use the syntax of deleting the index to delete the unique constraint.
ALTER TABLE dept DROP INDEX address;
or
ALTER TABLE emp CHANGE empno empno INT(11). -
Add a unique constraint to the created table
ALTER TABLE dept CHANGE dname dname VARCHAR(10) NOT NULL UNIQUE;
non
-null constraint The value of the column constrained by non-null cannot be NULL
DROP TABLE dept; -
When creating the table, add a non-null constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY,
-add a primary key constraint to the deptno field dname VARCHAR(10) NOT NULL,-add a non-null constraint to the DNAME field,
address VARCHAR(100)
) ;
INSERT INTO dept VALUES(10,'xxx','Dallas'); – Successful
INSERT INTO dept VALUES(20,'YYY','Dallas'); – Successful
INSERT INTO dept VALUES(30,NULL,'Dallas'); --Failed because of non-empty constraint violation
- Delete the non-empty constraint
ALTER TABLE dept CHANGE dname dname VARCHAR(10); - Add a non-null constraint to the created table
ALTER TABLE dept CHANGE dname dname VARCHAR(10) NOT NULL;
Default value
Set the default value constraint field, if not assigned, use the default value
DROP TABLE dept; - When creating the table, add the default value constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY,
-add the primary key constraint to the deptno field dname VARCHAR(10) NOT NULL,-add the non-null constraint to the DNAME field,
address VARCHAR(100) UNIQUE NOT NULL DEFAULT'Shanghai'-add unique and non-null constraints to address
);
INSERT INTO dept VALUES(10,'xxx','Dallas');
INSERT INTO dept (deptno,dname) VALUES(210,'xxxY');
INSERT INTO dept VALUES(30,'xxx','Dallas'); - Delete the default value constraint
ALTER TABLE dept CHANGE address address VARCHAR(10); - Add default value constraint
ALTER TABLE dept CHANGE address address VARCHAR(10) NOT NULL DEFAULT'Shanghai';
foreign key constraint
Is the constraint between the two tables. For example, use the a field in the A table to constrain the data in the b field in the B table, the A table is called the primary table [parent table], and the B table is called the secondary table [child table]. The primary key must be used to constrain the secondary table in the primary table.
DROP TABLE dept;
DROP TABLE emp;
CREATE TABLE dept(
deptno INT PRIMARY KEY, - Add a primary key constraint to the deptno column
dname VARCHAR(10),
address VARCHAR(100)
);
Add a foreign key constraint
CREATE TABLE emp(
empno INT,
deptnoX INT,
CONSTRAINT fk_dept_emp FOREIGN KEY (deptnoX) REFERENCES dept(deptno)
)
INSERT INTO dept VALUES(10,'xxx','Dallas');
INSERT INTO dept VALUES(30,'xxx','Dallas');
INSERT INTO emp VALUES(10,10);
INSERT INTO emp VALUES(30,30);
INSERT INTO emp VALUES(70,70);-Adding failed because there is no department 70 in the dept table of the main table
- Delete foreign key constraints
ALTER TABLE tableName DROP FOREIGN KEY fk_name;
ALTER TABLE emp DROP FOREIGN KEY fk_dept_emp;
Function
character function
- CHARSET(str) returns the string character set
- CONCAT (string2 [,… ]) connection string
- INSTR (string, substring) returns the position where the substring appears in the string, without returning 0
- UCASE (string2) converted to uppercase
- LCASE (string2) converted to lowercase
- LEFT (string2, length) Take length characters from the left in string2
- LENGTH (string) string length
- REPLACE (str ,search_str ,replace_str) Replace with replace_str in str-search_str
- STRCMP (string1, string2) compares the size of two strings character by character
- SUBSTRING (str, position [,length ]) Starting from the position of str, take length characters
- LTRIM (string2) RTRIM (string2) trim removes leading or trailing spaces
Mathematical function
- ABS (number2) absolute value
- BIN (decimal_number) decimal to binary
- CEILING (number2) round up
- CONV(number2,from_base,to_base) base conversion
- FLOOR (number2) round down
- FORMAT (number,decimal_places) to hexadecimal
- LEAST (number, number2 [,…]) find the minimum
- MOD (numerator ,denominator ) 求余
- RAND([seed]) RAND([seed])
Date and time functions
- ADDTIME (date2, time_interval) add time_interval to date2
- CURRENT_DATE () current date
- CURRENT_TIME ( )
- CURRENT_TIMESTAMP () Current time and
current timestamp - DATE (datetime) returns the date part of datetime
- DATE_ADD (date2, INTERVAL d_value d_type) add date or time to date2
- DATE_SUB (date2, INTERVAL d_value d_type) subtract a time from date2
- DATEDIFF (date1, date2) the difference between two dates
- NOW () current time
- YEAR|Month|DATE (datetime ) 年月日
System Information Function
Encryption function
Function role
- Password(str) is generally used to encrypt the user's password
- Md5(str) Encrypt common data
Other functions
- The CONVERT(S USING CS) function converts the character set of the string S into CS.
- Convert the encoding of the
string'ABC' to GBK. SELECT CHARSET('ABC'),CHARSET(CONVERT('ABC' USING'GBK'));