当前位置:网站首页>Common SQL statement collation: MySQL

Common SQL statement collation: MySQL

2022-07-07 11:24:00 Full stack programmer webmaster

## Commonly used sql Sentence arrangement :mysql

1. increase

– Add a table “` CREATE TABLE `table_name`( … )ENGINE=InnoDB DEFAULT CHARSET=utf8; “`

– Increase record “` INSERT INTO `your_table_name`(`column_name`) VALUES (‘your_value_one’), (‘your_value_two’); “`

– Add fields “` ALTER TABLE `your_table_name` ADD `your_column_name` … AFTER `column_name`; “`

– Add index + Primary key “` ALTER TABLE `your_table_name` ADD PRIMARY KEY your_index_name(your_column_name); “` + unique index “` ALTER TABLE `your_table_name` ADD UNIQUE your_index_name(your_column_name); “` + General index “` ALTER TABLE `your_table_name` ADD INDEX your_index_name(your_column_name); “` + Full-text index “` ALTER TABLE `your_table_name` ADD FULLTEXT your_index_name(your_column_name); “`

2. Delete

– Delete line by line “` DELETE FORM `table_name` WHERE …; “`

– Clear the whole table “` TRUNCATE TABLE `your_table_name`; “`

– Delete table “` DROP TABLE `your_table_name`; “`

– Delete field “` ALTER TABLE `your_table_name` DROP `column_name`; “`

– Delete index “` ALTER TABLE `your_table_name` DROP INDEX your_index_name(your_column_name); “`

3. Change

– Change data “` UPDATE `table_name` SET column_name=your_value WHERE …; “`

– Change field “` ALTER TABLE `your_table_name` CHANGE `your_column_name` `your_column_name` …( change ); “`

– Change the field value to a value in another table “` UPDATE `your_table_name` AS a JOIN `your_anther_table_name` AS b SET a.column = b.anther_column WHERE a.id = b.a_id…; “`

4. check

– Common query “` SELECT `column_name_one`, `column_name_two` FROM `table_name`; “`

– Relational query “` SELECT * FROM `your_table_name` AS a JOIN `your_anther_table_name` AS b WHERE a.column_name = b.column_name…; “`

– Total function condition query :WHERE Keyword cannot be used with aggregate function “` SELECT aggregate_function(column_name) FROM your_table_name GROUP BY column_name HAVING aggregate_function(column_name)…; “`

– Cross database query under the same instance “` SELECT * FROM database_name.your_table_name AS a JOIN another_database_name.your_another_table_name AS b WHERE a.column_name = b.column_name…; “`

5. Copy a table structure “` CREATE TABLE `your_table_name` LIKE `destination_table_name`; “`

6. Copy a table completely : Table structure + All data “` CREATE TABLE `your_table_name` LIKE `destination_table_name`;

INSERT INTO `your_table_name` SELECT * FROM `destination_table_name`; “`

### appendix :mysql Common commands – land : mysql -h host -u username -p – List databases :SHOW DATABESES; – List tables :SHOW TABLES; – List table structure :DESC table_name – Use a database :USE database_name; – Import :source ‘file’; – export :mysqldump -h 127.0.0.1 -u root -p “database_name” “table_name” –where=”condition” > file_name.sql; – Check the slow log :mysqldumpslow -s [c: Sort by number of records /t: Time /l: Lock time /r: Number of records returned ] -t [n: front n Data ] -g “ Regular ” /path – New users : insert into `user`(`Host`, `User`, `authentication_string`) value(‘localhost’, ‘username’, password(‘pwd’))

### mysql 5.7 New users

“` // Insert a new user insert into mysql.user(Host, User, authentication_string, ssl_cipher, x509_issuer, x509_subject value(‘localhost’, ‘username’, password(‘password’), ”, ”, ”);

// Database authorization grant all privileges on dbname.name.* to [email protected] identified by ‘password’;

// Refresh permission information FLUSH PRIVILEGES; “`

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113828.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070905458094.html