mysql [-h host] -u root -p connection MySQL
-h host: address of the host is the host to be connected may be omitted
-u root: root for the administrator user name in MySQL
-p: root password corresponding to the user
# Create a user
CREATE USER 'username' [@ 'Host Address'] [IDENTIFIED BY ' password '];
[]: To be partially omitted, if the host address is omitted by default %; if the password is omitted,
This user has no password !
# Create a MySQL user cmy
CREATE USER ‘cmy’@’%’ IDENTIFIED BY‘123’;
# Granting cmy users full access
GRANT ALL ON *.* TO ‘cmy’@’%’;
# Refresh permissions to take effect immediately
FLUSH PRIVILEGES;
#delete users
DROP USER 'username' @ 'Host address';
# Create a database `` the role of the database name | table names and keywords can have a separate area of the database whether
CREATE DATABASE 'database name';
# Display data
SHOW DATABASES;
# Switch databases
USE 'database name';
# Delete database
DROP DATABASE 'database name';
(1) modify the table name
ALTER TABLE old table name RENAME new table name ;
Data type (2) modify the field of
ALTER TABLE table MODIFY Field Name Data Type ;
(3) modify the field name
ALTER TABLE table name CHANGE old field name the new field name new data types ;
(4) Add Field
ALTER TABLE table name ADD new field name data type ;
(5) removing fields
ALTER TABLE table DROP field name ;
(6) to set the primary key table constraint constraint name: PK_ column name
ALTER TABLE table name ADD CONSTRAINT constraint name PRIMARY KEY table name ( ` column `)
(7) provided constraint foreign key to the table
ALTER TABLE Table ADD CONSTRAINT constraint name FOREIGN KEY ( ` the foreign key table ') the REFERENCES` primary table `(` primary key `);
(8) delete the data table (table structure and data is not retained)
DROP TABLE 表名;
(9) delete the data table (table structure to retain the data is not retained)
TRUNCATE 表名;
(10) revoke user privileges
revoke all on *.* from 'root'@'192.168.0.197' ;
(11) Delete Foreign Key
ALTER TABLE test2 DROP FOREIGN KEY FK_tnum;
(12) Drop Primary
ALTER TABLE test DROP PRIMARY KEY;
# Java query whether there is the basis of test scores of students is greater than 80 points, if there is a print all of
the SELECT stuname, the FROM Student S stuid the WHERE EXISTS (
the SELECT stuid, Score, the FROM sid the Result the WHERE sid = r
(the SELECT sid the FROM SUBJECT the WHERE sname = 'Java foundation')
the aND score> = 60 the aND s.`stuid` r.`stuid`
);
# Java query whether there is the basis of test scores of students more than 80 points, if there are three pre-print
SELECT s.stuname,s.stuid,r.score,r.sid FROM student s
INNER JOIN result r ON s.`stuid`=r.`stuid`
WHERE EXISTS(
SELECT stuid,score,sid FROM result r1 WHERE score >70
AND r1.sid=(SELECT sid FROM SUBJECT WHERE sname='Java基础')
AND r.`stuid`=r1.`stuid` AND r.`sid`=r1.`sid`
)ORDER BY score DESC LIMIT 3;
# Check Java Fundamentals exam results if there is less than 80 points, and if so, all grades +5 points until> = 80
UPDATE the SET the Result Score Score + 5 = the WHERE EXISTS (
the SELECT stuid, Score, sid the FROM (the FROM the SELECT * Result) the WHERE `sid` tmp =
(the FROM SUBJECT the WHERE sname the SELECT SID = 'basis the Java')
the AND tmp.score <= 80 the AND tmp.stuid result.`stuid` the AND tmp.sid = result.`sid`
);