MySQL日常操作

來源:互聯網
上載者:User

標籤:pat   安裝   ase   red   source   exp   signed   1.4   ora   

< <= >= > = between in 不帶%或者_開頭的like會使用索引操作

 

1、查看一個欄位在哪些表中:

use information_schema;

select table_name form columns where column_name=‘xxx‘

2、查看建表語句

show create table tablename

3、修改列的資料類型

alert table 表名 modify column 列名 新的列的類型

alert table example modify column col1 varchar(10)

只修改列名,或者同時修改列明和列的資料類型

alert table 表名 change column 舊列名 新列名 新的列類型

alert table example change column sname stuname varchar(10)

4、This version of MySQL doesn‘t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery‘的意思是,

這版本的 MySQL 不支援使用 LIMIT 子句的 IN/ALL/ANY/SOME 子查詢,即是支援非 IN/ALL/ANY/SOME 子查詢的 LIMIT 子查詢。

也就是說,這樣的語句是不能正確執行的。 

select * from table where id in (select id from table limit 10)

但是,只要你再來一層就行。。如: 

select * from table where id in (select t.id from (select * from table limit 10)as t)

5、三張表join

select xxx from (( A left join B on A.id = B.id)

left join C on A.id = C.id

where B.id is not null

select * from A inner join B on A.id = A.id inner join C on B.id = C.id

6、增加一個欄位

alert table tablename add column fieldname dataType

7、複製表

create table table2 select * from table1

對錶重新命名

alert table table1 rename as table2

修改列的類型

alert table table1 modify id int unsigned

建立索引

create (unique唯一索引)index ide_id on table1 (id)

刪除索引

drop index inx_id on table1

8、linux進入MySQL方式:

1) 進入MySQL安裝目錄下的bin檔案夾下,執行./mysql -uroot -p

2)export PATH=/usr/local/mysql/bin:$PATH 這種方法在終端關閉

的時候會失效。

3)vim /ect/profile

在最後添加: export PATH="/usr/local/mysql/bin:$PATH"儲存,退出

source /ect/profile 不報錯則成功

 

1、查看資料庫的串連數:

SELECT summary_id,COUNT(*) AS COUNT FROM pe_project_summary_basic GROUP BY summary_id HAVING COUNT>1;

 

摘錄於MySQL官網 修改表/資料庫字元編碼alter database mydb character set utf-8;alter table conf_dictitem convert to character set utf8 4、外鍵樣本CREATE TABLE person ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, name CHAR(60) NOT NULL, PRIMARY KEY (id) ); CREATE TABLE shirt ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, style ENUM(‘t-shirt‘, ‘polo‘, ‘dress‘) NOT NULL, color ENUM(‘red‘, ‘blue‘, ‘orange‘, ‘white‘, ‘black‘) NOT NULL, owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id), PRIMARY KEY (id) ); INSERT INTO person VALUES (NULL, ‘Antonio Paz‘); SELECT @last := LAST_INSERT_ID(); INSERT INTO shirt VALUES (NULL, ‘polo‘, ‘blue‘, @last), (NULL, ‘dress‘, ‘white‘, @last), (NULL, ‘t-shirt‘, ‘blue‘, @last); INSERT INTO person VALUES (NULL, ‘Lilliana Angelovska‘); SELECT @last := LAST_INSERT_ID(); INSERT INTO shirt VALUES (NULL, ‘dress‘, ‘orange‘, @last), (NULL, ‘polo‘, ‘red‘, @last), (NULL, ‘dress‘, ‘blue‘, @last), (NULL, ‘t-shirt‘, ‘white‘, @last); SELECT s.* FROM person p INNER JOIN shirt s ON s.owner = p.id WHERE p.name LIKE ‘Lilliana%‘ AND s.color <> ‘white‘; 3、SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop; SELECT * FROM shop WHERE [email protected]_price OR [email protected]_price; 1、 匹配語句SELECT * FROM pet WHERE name REGEXP ‘w‘;包含w字元, ^w以w開頭的字元, w$以w結尾的字元, ‘^.....$‘ 長度為五個字元(如果是中文的話,一個中文為3個...,即一個中文佔三個字元), ‘^.{6}$‘ 同上一個一樣的作用 2、LOAD DATA LOCAL INFILE ‘event.txt‘ INTO TABLE event 將TXT檔案匯入event表中在Linux執行sql檔案,source xxx.sql CREATE TABLE shop ( article INT(4) UNSIGNED ZEROFILL DEFAULT ‘0000‘ NOT NULL, dealer CHAR(20) DEFAULT ‘‘ NOT NULL, price DOUBLE(16,2) DEFAULT ‘0.00‘ NOT NULL, PRIMARY KEY(article, dealer)); INSERT INTO shop VALUES (1,‘A‘,3.45),(1,‘B‘,3.99),(2,‘A‘,10.99),(3,‘B‘,1.45), (3,‘C‘,1.69),(3,‘D‘,1.25),(4,‘D‘,19.95);

 

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.