當前位置:首頁 » 編程語言 » sql獲取資料庫表

sql獲取資料庫表

發布時間: 2022-12-18 03:38:30

❶ 怎樣用sql語句查詢一個資料庫中的所有表

TABLE 語句

具體語法:TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]
其實從語法上看,可以排序,也可以過濾記錄集,不過比較簡單,沒有 SELECT 那麼強大。

示例 1

簡單的建一張很小的表 y1,記錄數為 10 條。表 t1,插入 10 條記錄

  • mysql-(ytt/3305)->create table t1 (r1 int,r2 int);

  • Query OK, 0 rows affected (0.02 sec)

  • mysql-(ytt/3305)->insert into t1

  • with recursive aa(a,b) as (

  • select 1,1

  • union all

  • select a+1,ceil(rand()*20) from aa where a < 10

  • ) select * from aa;

  • Query OK, 10 rows affected (0.00 sec)

  • Records: 10 Duplicates: 0 Warnings: 0

  • 簡單全表掃描mysql-(ytt/3305)->select * from t1;+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)

  • TABLE 結果mysql-(ytt/3305)->table t1;+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)

  • 看下 table 的執行計劃mysql-(ytt/3305)->explain table t1 order by r1 limit 2G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 10 filtered: 100.00 Extra: Using filesort1 row in set, 1 warning (0.00 sec)

  • 其實可以看到 TABLE 內部被 MySQL 轉換為 SELECT 了。mysql-(ytt/3305)->show warningsG*************************** 1. row *************************** Level: Note Code: 1003Message: /* select#1 */ select `ytt`.`t1`.`r1` AS `r1`,`ytt`.`t1`.`r2` AS `r2` from `ytt`.`t1` order by `ytt`.`t1`.`r1` limit 21 row in set (0.00 sec)

  • 那其實從上面簡單的例子可以看到 TABLE 在內部被轉成了普通的 SELECT 來處理。示例 2應用於子查詢里的子表。這里要注意,內表的欄位數量必須和外表過濾的欄位數量一致。克隆表 t1 結構mysql-(ytt/3305)->create table t2 like t1;Query OK, 0 rows affected (0.02 sec)

  • 克隆表 t1 數據mysql-(ytt/3305)->insert into t2 table t1;Query OK, 10 rows affected (0.00 sec)Records: 10 Duplicates: 0 Warnings: 0

  • table t1 被當做內表,表 t1 有兩個欄位,必須同時滿足 t2 檢索時過濾的欄位也是兩個。mysql-(ytt/3305)->select * from t2 where (r1,r2) in (table t1);+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)

  • 注意:這里如果過濾的欄位數量和子表數量不一致,則會報錯。

❷ 怎樣用SQL語句查詢一個資料庫中的所有表

1、打開Microsoft SQL Server 2012,選中需要查詢所有表的資料庫。

❸ sql查詢資料庫中有某個值的所有表

1、首先在電腦中打開Microsoft SQL Server,查詢所有資料庫。

❹ 如何通過sql獲取資料庫所有表數據

1.查詢資料庫中的所有資料庫名:
SELECT Name FROM Master..SysDatabases ORDER BY Name

2.查詢某個資料庫中所有的表名:
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name

3.查詢表結構信息:

1 SELECT (case when a.colorder=1 then d.name else null end) 表名,
2 a.colorder 欄位序號,a.name 欄位名,
3 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 標識,
4 (case when (SELECT count(*) FROM sysobjects
5 WHERE (name in (SELECT name FROM sysindexes
6 WHERE (id = a.id) AND (indid in
7 (SELECT indid FROM sysindexkeys
8 WHERE (id = a.id) AND (colid in
9 (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
10 AND (xtype = 'PK'))>0 then '√' else '' end) 主鍵,b.name 類型,a.length 佔用位元組數,
11 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 長度,
12 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小數位數,(case when a.isnullable=1 then '√'else '' end) 允許空,
13 isnull(e.text,'') 默認值,isnull(g.[value], ' ') AS [說明]
14 FROM syscolumns a
15 left join systypes b on a.xtype=b.xusertype
16 inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
17 left join syscomments e on a.cdefault=e.id
18 left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
19 left join sys.extended_properties f on d.id=f.class and f.minor_id=0
20 where b.name is not null
21 --WHERE d.name='要查詢的表' --如果只查詢指定表,加上此條件
22 order by a.id,a.colorder

❺ 怎樣用SQL語句查詢一個資料庫中的所有表

查詢資料庫里所有表名和欄位名的語句

SQL 查詢所有表名:

SELECT NAME FROM SYSOBJECTS WHERE TYPE='U'

SELECT * FROM INFORMATION_SCHEMA.TABLES

結構化查詢語言(Structured Query Language)簡稱SQL,結構化查詢語言是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統;

sql 語句就是對資料庫進行操作的一種語言。

(5)sql獲取資料庫表擴展閱讀:

SQL語句常見語句:

1、更新:update table1 set field1=value1 where 范圍;

2、查找:select * from table1 where field1 like 』%value1%』 (所有包含『value1』這個模式的字元串);

3、排序:select * from table1 order by field1,field2 [desc];

4、求和:select sum(field1) as sumvalue from table1;

5、平均:select avg(field1) as avgvalue from table1;

6、最大:select max(field1) as maxvalue from table1;

7、最小:select min(field1) as minvalue from table1[separator]。

參考資料來源:網路-sql語句

熱點內容
dirt5需要什麼配置 發布:2024-05-20 06:02:58 瀏覽:542
怎麼把電腦鎖上密碼 發布:2024-05-20 05:19:09 瀏覽:985
安卓為什麼連上wifi後沒有網路 發布:2024-05-20 05:17:50 瀏覽:419
安卓usb在設置哪裡 發布:2024-05-20 05:03:03 瀏覽:187
綏化編程 發布:2024-05-20 04:59:44 瀏覽:991
基本原理和從頭計演算法 發布:2024-05-20 04:50:32 瀏覽:30
配置情況指的是什麼 發布:2024-05-20 04:48:14 瀏覽:497
那個程序用來編譯源文件 發布:2024-05-20 04:46:45 瀏覽:551
小程序需要資料庫嗎 發布:2024-05-20 04:35:14 瀏覽:338
鏈接sqlserver 發布:2024-05-20 04:27:53 瀏覽:210