您可以通過提供另一個名稱(ALIAS)來臨時重命名表或列。 表別名的使用意味著在特定的SQLite語句中重命名表。 重命名是一個臨時更改,數(shù)據(jù)庫中的實際表名不會更改。
列別名用于重命名表的列,以用于特定的SQLite查詢。
以下是table別名的基本語法。
SELECT column1, column2.... FROM table_name AS alias_name WHERE [condition];
以下是column別名的基本語法。
SELECT column_name AS alias_name FROM table_name WHERE [condition];
考慮以下兩個表,(a)COMPANY表如下-
sqlite> select * from COMPANY; ID NAME AGE ADDRESS SALARY ---------- -------------------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
(b)另一個表格是部門--
ID DEPT EMP_ID ---------- -------------------- ---------- 1 IT Billing 1 2 Engineering 2 3 Finance 7 4 Engineering 3 5 Finance 4 6 Engineering 5 7 Finance 6
現(xiàn)在,下面是TABLE ALIAS我們分別使用C和D作為COMPANY和DEPARTMENT表的別名的用法-
sqlite> SELECT C.ID, C.NAME, C.AGE, D.DEPT FROM COMPANY AS C, DEPARTMENT AS D WHERE C.ID = D.EMP_ID;
上面的SQLite語句將產(chǎn)生以下結(jié)果-
ID NAME AGE DEPT ---------- ---------- ---------- ---------- 1 Paul 32 IT Billing 2 Allen 25 Engineering 3 Teddy 23 Engineering 4 Mark 25 Finance 5 David 27 Engineering 6 Kim 22 Finance 7 James 24 Finance
考慮一個使用示例,COLUMN ALIAS其中COMPANY_ID是ID列的別名,而COMPANY_NAME是name列的別名。
sqlite> SELECT C.ID AS COMPANY_ID, C.NAME AS COMPANY_NAME, C.AGE, D.DEPT FROM COMPANY AS C, DEPARTMENT AS D WHERE C.ID = D.EMP_ID;
上面的SQLite語句將產(chǎn)生以下結(jié)果-
COMPANY_ID COMPANY_NAME AGE DEPT ---------- ------------ ---------- ---------- 1 Paul 32 IT Billing 2 Allen 25 Engineering 3 Teddy 23 Engineering 4 Mark 25 Finance 5 David 27 Engineering 6 Kim 22 Finance 7 James 24 Finance