mysql特殊處理

來源:互聯網
上載者:User

標籤:

 

 

mysql> create table ef (bc time);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into ef values (current_timestamp);
Query OK, 1 row affected (0.00 sec)

mysql> select * from ef;
+----------+
| bc       |
+----------+
| 11:33:26 |
| 11:33:35 |
| 11:33:39 |
+----------+
3 rows in set (0.00 sec)


mysql> create table ii(qq datetime);

由於MySQL目前欄位的預設值不支援函數,所以以create_time datetime default now() 的形式設定預設值是不可能的。代替的方案是使用TIMESTAMP類型代替DATETIME類型。
CREATE TABLE test (   
uname varchar(50) NOT NULL,   
updatetime timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP   
) ENGINE=InnoDB DEFAULT CHARSET=utf8;   

MySQL中,預設值無法使用函數也就是你無法設定某一列,預設值是 NOW () 這樣的處理
假如需要某列的預設值為當前資料庫時間,那麼可以使用 TIMESTAMP 資料類型。插入的時候,填寫 null 即可。

mysql> create table testA ( dt TIMESTAMP );
Query OK, 0 rows affected (0.09 sec)

mysql> insert into testA VALUES( null );
Query OK, 1 row affected (0.01 sec)

mysql> insert into testA VALUES( null );
Query OK, 1 row affected (0.08 sec)

mysql> select * from testA;
+---------------------+
| dt                  |
+---------------------+
| 2011-10-15 20:30:35 |
| 2011-10-15 20:30:36 |
+---------------------+
2 rows in set (0.00 sec)

 

 

 


select * from table1 where 1=1與select * from table1完全沒有區別,甚至還有其他許多寫法,1<>2,‘a‘=‘a‘,‘a‘<>‘b‘,其目的就只有一個,where 的條件為永真,得到的結果就是未加約束條件的。

如果不寫1=1呢,那麼在每一個不為空白的查詢條件面前,都必須判斷有沒有where字句,否則要在第一個出現的地方加where
where 1=1的寫法是為了檢化程式中對條件的檢測
打個比方有三個參數a, b, c
@sql=select * from tb‘
這三個參數都可能為空白
這時你要構造語句的話,一個個檢測再寫語句就麻煩

 

select * from account where username like "%leo.lee%";
update account set state = 1 where state != 1
select count(*) from account where state != 1;

 

 

FEDERATED是其中一個專門針對遠端資料庫的實現。一般情況下在本機資料庫中建表會在資料庫目錄中產生相應的表定義檔案,並同時產生相應的資料檔案。
但通過FEDERATED引擎建立的表只是在本地有表定義檔案,資料檔案則存在於遠端資料庫中(這一點很重要)。
使用FEDERATED建表語句如下:
CREATE TABLE (......) ENGINE =FEDERATED CONNECTION=‘mysql://[name]:[pass]@[location]:[port]/[db-name]/[table-name]‘
建立成功後就可直接在本地查詢相應的遠端資料表了。

需要注意的幾點:

1.本地的表結構必須與遠端完全一樣。
2.遠端資料庫目前僅限MySQL
3.不支援事務
4.不支援表結構修改

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.