MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / CreateView

Class CreateView

lib/sqlalchemy/sql/ddl.py:758–895  ·  view source on GitHub ↗

Represent a CREATE VIEW statement. This creates a new view based on a particular SELECT statement. The schema of the view is based on the columns of the SELECT statement, and the data present in the view is derived from the rows represented by the SELECT. A non-materialized view wi

Source from the content-addressed store, hash-verified

756
757
758class CreateView(DialectKWArgs, _TableViaSelect):
759 """Represent a CREATE VIEW statement.
760
761 This creates a new view based on a particular SELECT statement. The schema
762 of the view is based on the columns of the SELECT statement, and the data
763 present in the view is derived from the rows represented by the
764 SELECT. A non-materialized view will evaluate the SELECT statement
765 dynamically as it is queried, whereas a materialized view represents a
766 snapshot of the SELECT statement at a particular point in time and
767 typically needs to be refreshed manually using database-specific commands.
768
769 The example below illustrates basic use of :class:`.CreateView`; given a
770 :class:`.Select` and optional :class:`.MetaData`, the
771 :class:`.CreateView` may be invoked directly via
772 :meth:`.Connection.execute` or indirectly via :meth:`.MetaData.create_all`;
773 the :attr:`.CreateView.table` attribute provides a :class:`.Table`
774 object with which to generate new queries::
775
776
777 from sqlalchemy import select
778 from sqlalchemy.sql.ddl import CreateView
779
780 # instantiate CreateView given a select() and optional MetaData
781 create_view = CreateView(
782 select(users.c.id, users.c.name).where(users.c.status == "active"),
783 "active_users_view",
784 metadata=some_metadata,
785 )
786
787 # a Table object is available immediately via the .table attribute
788 new_statement = select(create_view.table)
789
790 # to emit CREATE VIEW, either invoke CreateView directly...
791 with engine.begin() as conn:
792 conn.execute(create_view)
793
794 # or alternatively, invoke metadata.create_all()
795 some_metdata.create_all(engine)
796
797 # drop is performed in the usual way, via drop_all
798 # or table.drop() (will emit DROP VIEW)
799 some_metdata.drop_all(engine)
800
801 For detailed background on :class:`.CreateView` see
802 :ref:`metadata_create_view`.
803
804 .. versionadded:: 2.1
805
806 :param selectable: :class:`_sql.Select`
807 The SELECT statement defining the view.
808
809 :param view_name: table name as a string. Combine with the optional
810 :paramref:`.CreateView.schema` parameter to indicate a
811 schema-qualified table name.
812
813 :param metadata: :class:`_schema.MetaData`, optional
814 If provided, the :class:`_schema.Table` object available via the
815 :attr:`.table` attribute will be associated with this

Calls

no outgoing calls