1. Trang chủ
  2. » Công Nghệ Thông Tin

SQL VISUAL QUICKSTART GUIDE- P47 ppt

10 284 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 167,26 KB

Nội dung

Microsoft Access Access metadata are available graphically through the Design View of each database object and programmatically through the Visual Basic for Applications (VBA) or C# language. Access also creates and maintains hidden system tables in each database. To show system tables: ◆ In Access 2003 or earlier, choose Tools > Options > View tab > check System Objects. or In Access 2007 or later, choose Microsoft Office button > Access Options > Current Database (in the left pane) > scroll to Navigation > click Navigation Options > check Show System Objects. The system tables begin with MSys and are commingled with the database’s other tables. You can open and query them as you would ordinary tables. The most interesting system table is MSysObjects , which catalogs all the objects in the database. Listing 15.39 lists all the tables in the current database. Note that system tables don’t have to be visi- ble to be used in queries. Microsoft SQL Server SQL Server metadata are available through the schema INFORMATION_SCHEMA and via sys- tem stored procedures (Listing 15.40). 440 Chapter 15 Retrieving Metadata Listing 15.39 List the tables in the current Access database. To list queries instead, change Type = 1 to Type = 5 . SELECT Name FROM MSysObjects WHERE Type = 1; Listing Listing 15.40 Metadata statements and commands for Microsoft SQL Server. List the databases. exec sp_helpdb; List the schemas. SELECT schema_name FROM information_schema.schemata; List the tables (Method 1). SELECT * FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = 'schema_name'; List the tables (Method 2). exec sp_tables; Describe a table (Method 1). SELECT * FROM information_schema.columns WHERE table_catalog = 'db_name' AND table_schema = 'schema_name' AND table_name = 'table_name'; Describe a table (Method 2). exec sp_help table_name; Listing Oracle Oracle metadata are available through data dictionary views and via sqlplus (Listing 15.41). To list data dictionary views, run this query in sqlplus : SELECT table_name, comments FROM dictionary ORDER BY table_name; For a list of Oracle databases (instances) in Unix or Linux, look in the file oratab located in the directory /etc or /var/opt/oracle . In Windows, run this command at a com- mand prompt: net start | find /i "OracleService" Or choose Start > Run (Windows logo key+R), type services.msc , press Enter, and then inspect the Services list for entries that begin with OracleService. 441 SQL Tricks Retrieving Metadata Listing 15.41 Metadata statements and commands for Oracle. List the schemas (users). SELECT * FROM all_users; List the tables. SELECT table_name FROM all_tables WHERE owner = 'user_name'; Describe a table (Method 1). SELECT * FROM all_tab_columns WHERE owner = 'user_name' AND table_name = 'table_name'; Describe a table (Method 2, in sqlplus). DESCRIBE table_name; Listing IBM DB2 DB2 metadata are available through the system catalog SYSCAT and via db2 (Listing 15.42). 442 Chapter 15 Retrieving Metadata Listing 15.42 Metadata statements and commands for IBM DB2. List the databases (in db2). LIST DATABASE DIRECTORY; List the schemas. SELECT schemaname FROM syscat.schemata; List the tables (Method 1). SELECT tabname FROM syscat.tables WHERE tabschema = 'schema_name'; List the tables (Method 2, in db2). LIST TABLES; List the tables (Method 3, in db2). LIST TABLES FOR SCHEMA schema_name; Describe a table (Method 1). SELECT * FROM syscat.columns WHERE tabname = 'table_name' AND tabschema = 'schema_name'; Describe a table (Method 2, in db2). DESCRIBE TABLE table_name SHOW DETAIL; Listing MySQL MySQL metadata are available through the schema INFORMATION_SCHEMA and via mysql (Listing 15.43). 443 SQL Tricks Retrieving Metadata Listing 15.43 Metadata statements and commands for MySQL. List the databases (Method 1). SELECT schema_name FROM information_schema.schemata; List the databases (Method 2, in mysql). SHOW DATABASES; List the tables (Method 1). SELECT table_name FROM information_schema.tables WHERE table_schema = 'db_name'; List the tables (Method 2, in mysql). SHOW TABLES; Describe a table (Method 1). SELECT * FROM information_schema.columns WHERE table_schema = 'db_name' AND table_name = 'table_name'; Describe a table (Method 2, in mysql). DESCRIBE table_name; Listing PostgreSQL PostgreSQL metadata are available through the schema INFORMATION_SCHEMA and via psql (Listing 15.44). 444 Chapter 15 Retrieving Metadata Listing 15.44 Metadata statements and commands for PostgreSQL. List the databases (Method 1). psql list List the databases (Method 2, in psql). \l List the schemas. SELECT schema_name FROM information_schema.schemata; List the tables (Method 1). SELECT table_name FROM information_schema.tables WHERE table_schema = 'schema_name'; List the tables (Method 2, in psql). \dt Describe a table (Method 1). SELECT * FROM information_schema.columns WHERE table_schema = 'schema_name' AND table_name = 'table_name'; Describe a table (Method 2, in psql). \d table_name; Listing Working with Dates As pointed out in “Getting the Current Date and Time” and “Performing Datetime and Interval Arithmetic” in Chapter 5, DBMSs provide their own extended (nonstandard) functions for manipulating dates and times. This section explains how to use these built- in functions to do simple date arithmetic. The queries in each listing: ◆ Extract parts (hour, day, month, and so on) of the current (system) datetime and return them as numbers. ◆ Add and subtract intervals of days, months, and years from a date. ◆ Count the days between two dates in dif- ferent rows of the same column. The result is positive, zero, or negative depending on whether the first date falls before, on, or after the second date. ◆ Count the months between the earliest and latest dates in the same column. Microsoft Access The function datepart() extracts the speci- fied part of a datetime. now() returns the current (system) date and time. dateadd() adds a specified time interval to a date. datediff() returns the number of speci- fied time intervals between two dates (Listing 15.45). ✔ Tip ■ Alternatives to datepart() are the extraction functions second() , day() , month() , and so on. 445 SQL Tricks Working with Dates Listing 15.45 Working with dates in Microsoft Access. Extract parts of the current datetime. SELECT datepart("s", now()) AS sec_pt, datepart("n", now()) AS min_pt, datepart("h", now()) AS hr_pt, datepart("d", now()) AS day_pt, datepart("m", now()) AS mon_pt, datepart("yyyy",now()) AS yr_pt; Add or subtract days, months, and years. SELECT dateadd("d", 2,pubdate) AS p2d, dateadd("d", -2,pubdate) AS m2d, dateadd("m", 2,pubdate) AS p2m, dateadd("m", -2,pubdate) AS m2m, dateadd("yyyy", 2,pubdate) AS p2y, dateadd("yyyy",-2,pubdate) AS m2y FROM titles WHERE title_id = 'T05'; Count the days between two dates. SELECT datediff("d",date1,date2) AS days FROM (SELECT pubdate as date1 FROM titles WHERE title_id = 'T05') t1, (SELECT pubdate as date2 FROM titles WHERE title_id = 'T06') t2; Count the months between two dates. SELECT datediff("m",date1,date2) AS months FROM (SELECT MIN(pubdate) AS date1, MAX(pubdate) AS date2 FROM titles) t1; Listing Microsoft SQL Server The function datepart() extracts the speci- fied part of a datetime. getdate() returns the current (system) date and time. dateadd() adds a specified time interval to a date. datediff() returns the number of specified time intervals between two dates (Listing 15.46). ✔ Tip ■ Alternatives to datepart() are the extraction functions day() , month() , and year() . 446 Chapter 15 Working with Dates Listing 15.46 Working with dates in Microsoft SQL Server. Extract parts of the current datetime. SELECT datepart("s", getdate()) AS sec_pt, datepart("n", getdate()) AS min_pt, datepart("hh", getdate()) AS hr_pt, datepart("d", getdate()) AS day_pt, datepart("m", getdate()) AS mon_pt, datepart("yyyy",getdate()) AS yr_pt; Add or subtract days, months, and years. SELECT dateadd("d", 2,pubdate) AS p2d, dateadd("d", -2,pubdate) AS m2d, dateadd("m", 2,pubdate) AS p2m, dateadd("m", -2,pubdate) AS m2m, dateadd("yyyy", 2,pubdate) AS p2y, dateadd("yyyy",-2,pubdate) AS m2y FROM titles WHERE title_id = 'T05'; Count the days between two dates. SELECT datediff("d",date1,date2) AS days FROM (SELECT pubdate as date1 FROM titles WHERE title_id = 'T05') t1, (SELECT pubdate as date2 FROM titles WHERE title_id = 'T06') t2; Count the months between two dates. SELECT datediff("m",date1,date2) AS months FROM (SELECT MIN(pubdate) AS date1, MAX(pubdate) AS date2 FROM titles) t1; Listing Oracle The function to_char() converts a datetime to a character value in the given format. to_number() converts its argument to a number. sysdate returns the current (sys- tem) date and time. The standard addition and subtraction operators add and subtract days from a date. add_months() adds a speci- fied number of months to a date. Subtracting one date from another yields the number of days between them. months_between() returns the number of months between two dates (Listing 15.47). 447 SQL Tricks Working with Dates Listing 15.47 Working with dates in Oracle. Extract parts of the current datetime. SELECT to_number(to_char(sysdate,'ss')) AS sec_pt, to_number(to_char(sysdate,'mi')) AS min_pt, to_number(to_char(sysdate,'hh24')) AS hr_pt, to_number(to_char(sysdate,'dd')) AS day_pt, to_number(to_char(sysdate,'mm')) AS mon_pt, to_number(to_char(sysdate,'yyyy')) AS yr_pt FROM dual; Add or subtract days, months, and years. SELECT pubdate+2 AS p2d, pubdate-2 AS m2d, add_months(pubdate,+2) AS p2m, add_months(pubdate,-2) AS m2m, add_months(pubdate,+24) AS p2y, add_months(pubdate,-24) AS m2y FROM titles WHERE title_id = 'T05'; Count the days between two dates. SELECT date2 - date1 AS days FROM (SELECT pubdate as date1 FROM titles WHERE title_id = 'T05') t1, (SELECT pubdate as date2 FROM titles WHERE title_id = 'T06') t2; Count the months between two dates. SELECT months_between(date2,date1) AS months FROM (SELECT MIN(pubdate) AS date1, MAX(pubdate) AS date2 FROM titles) t1; Listing IBM DB2 The functions second() , day() , month() , and so on, extract part of a datetime. current_timestamp returns the current (system) date and time. The standard addition and subtraction operators add and subtract time intervals from a date. days() converts a date to an integer serial number (Listing 15.48). 448 Chapter 15 Working with Dates Listing 15.48 Working with dates in IBM DB2. Extract parts of the current datetime. SELECT second(current_timestamp) AS sec_pt, minute(current_timestamp) AS min_pt, hour(current_timestamp) AS hr_pt, day(current_timestamp) AS day_pt, month(current_timestamp) AS mon_pt, year(current_timestamp) AS yr_pt FROM SYSIBM.SYSDUMMY1; Add or subtract days, months, and years. SELECT pubdate + 2 DAY AS p2d, pubdate - 2 DAY AS m2d, pubdate + 2 MONTH AS p2m, pubdate - 2 MONTH AS m2m, pubdate + 2 YEAR AS p2y, pubdate - 2 YEAR AS m2y FROM titles WHERE title_id = 'T05'; Count the days between two dates. SELECT days(date2) - days(date1) AS days FROM (SELECT pubdate as date1 FROM titles WHERE title_id = 'T05') t1, (SELECT pubdate as date2 FROM titles WHERE title_id = 'T06') t2; Count the months between two dates. SELECT (year(date2)*12 + month(date2)) - (year(date1)*12 + month(date1)) AS months FROM (SELECT MIN(pubdate) AS date1, MAX(pubdate) AS date2 FROM titles) t1; Listing MySQL The function date_format() formats a datetime according to the specified format. current_timestamp returns the current (system) date and time. The standard addi- tion and subtraction operators add and sub- tract time intervals from a date. datediff() returns the number of days between two dates (Listing 15.49). ✔ Tip ■ Alternatives to date_format() are the extraction functions extract() , second() , day() , month() , and so on. 449 SQL Tricks Working with Dates Listing 15.49 Working with dates in MySQL. Extract parts of the current datetime. SELECT date_format(current_timestamp,'%s') AS sec_pt, date_format(current_timestamp,'%i') AS min_pt, date_format(current_timestamp,'%k') AS hr_pt, date_format(current_timestamp,'%d') AS day_pt, date_format(current_timestamp,'%m') AS mon_pt, date_format(current_timestamp,'%Y') AS yr_pt; Add or subtract days, months, and years. SELECT pubdate + INTERVAL 2 DAY AS p2d, pubdate - INTERVAL 2 DAY AS m2d, pubdate + INTERVAL 2 MONTH AS p2m, pubdate - INTERVAL 2 MONTH AS m2m, pubdate + INTERVAL 2 YEAR AS p2y, pubdate - INTERVAL 2 YEAR AS m2y FROM titles WHERE title_id = 'T05'; Count the days between two dates. SELECT datediff(date2,date1) AS days FROM (SELECT pubdate as date1 FROM titles WHERE title_id = 'T05') t1, (SELECT pubdate as date2 FROM titles WHERE title_id = 'T06') t2; Count the months between two dates. SELECT (year(date2)*12 + month(date2)) - (year(date1)*12 + month(date1)) AS months FROM (SELECT MIN(pubdate) AS date1, MAX(pubdate) AS date2 FROM titles) t1; Listing . db2). DESCRIBE TABLE table_name SHOW DETAIL; Listing MySQL MySQL metadata are available through the schema INFORMATION_SCHEMA and via mysql (Listing 15.43). 443 SQL Tricks Retrieving Metadata Listing 15.43. Describe a table (Method 2, in mysql). DESCRIBE table_name; Listing PostgreSQL PostgreSQL metadata are available through the schema INFORMATION_SCHEMA and via psql (Listing 15.44). 444 Chapter. Metadata Listing 15.44 Metadata statements and commands for PostgreSQL. List the databases (Method 1). psql list List the databases (Method 2, in psql). l List the schemas. SELECT schema_name FROM information_schema.schemata;

Ngày đăng: 05/07/2014, 05:20