mysql常用命令和記錄

來源:互聯網
上載者:User

標籤:常用命令   insert   user   csdn   using   name   語句   gone   sql   


mysql版本:Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64) using  EditLine wrapper
文檔:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html、http://www.runoob.com/mysql/mysql-index.html

mac下:
登陸:mysql -u root -p
查看有哪些資料庫:show databases;
選擇資料庫:use 資料庫名稱,例如“use zml_db;”
  建立一個資料庫MYSQLDATA:mysql> Create DATABASE MYSQLDATA;
  刪除一個資料庫:drop database <資料庫名>
查詢有哪些表:show tables;
建立一張表:Create TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));
刪除一張表:DROP TABLE MYTABLE;
清空一張表:delete from <表名>;truncate table <表名>; 區別請看http://blog.csdn.net/longshenlmj/article/details/18309529
顯示表的結構:mysql> describe <表名>;
表中增加欄位:alter table 表名 add (COLUMN) 欄位名 類型 其他; 例如"alter table mytable add age int default ‘0‘;"
表中刪除欄位:alter table 表名 drop (COLUMN) 欄位名;alter table mytable DROP COLUMN age;      
表中修改欄位(名、類型等):
  alter table user MODIFY new1 VARCHAR(10);            //修改一個欄位的類型
  alter table user CHANGE new1 new4 int;             //修改一個欄位的名稱,此時一定要重新指定該欄位的類型
更改表名:rename table 原表名 to 新表名;
基本的表資料的增刪查改語句:
  插入:insert into <表名> [( <欄位名1>[,..<欄位名n > ])] values ( 值1 )[, ( 值n )]
  刪除:delete from 表名 where 運算式
  查詢:select * from <表名> where ....
  更新:update <表名> set sex="f" where name=‘hyq‘;

分頁查詢:查詢前幾行資料。例如:查看錶 MyClass 中前2行資料mysql> select * from MyClass order by id limit 0,2;
建立索引:CREATE INDEX index_name ON table_name (column_name);//還有其他集中建立方式,見http://wiki.jikexueyuan.com/project/sql/indexes.html
——註:資料量不大的情況下沒必要添加索引,反而會降低增改刪的效率
三種常用的MySQL建表語句:http://database.51cto.com/art/201011/234350.htm

便捷操作:
用文本方式將資料裝入資料庫表中(例如D:/mysql.txt):mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE;

迴圈插入資料,採用預存程序來做,例如:

DROP PROCEDURE if exists myproc ;delimiter //create procedure myproc() begin declare num int; set num=1; while num < 100000 do insert into mytable (name,sex,age) values (CONCAT("zhang",num),"male",num); set num=num+1;end while;end//CALL myproc();

 

隨機查詢某張表中的一個資料:http://blog.csdn.net/mengxiangone/article/details/8093347,最終選擇“SELECT * FROM users WHERE userId >= ((SELECT MAX(userId) FROM users)-(SELECT MIN(userId) FROM users)) * RAND() + (SELECT MIN(userId) FROM users) LIMIT 1”

建索引的幾大原則:https://tech.meituan.com/mysql-index.html

經驗案例:
  模糊查詢最佳化三種方式:http://blog.csdn.net/createment/article/details/50602529

mysql常用命令和記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.