Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Bagaimana cara membuat Tabel Oracle yang Dipartisi di SQLAlchemy?

Inilah jawaban terbaik, terima kasih kepada Mike Byer

from sqlalchemy import MetaData, Column, String, create_engine
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.schema import CreateTable
from sqlalchemy.ext.compiler import compiles
import textwrap


@compiles(CreateTable, "oracle")
def _add_suffixes(element, compiler, **kw):
     text = compiler.visit_create_table(element, **kw)
     if "oracle_partition" in element.element.info:
         text += textwrap.dedent(
             element.element.info["oracle_partition"]).strip()
     return text 

# use mock strategy just to illustrate this w/o my getting
# on an oracle box
def execute_sql(stmt):
    print stmt.compile(dialect=engine.dialect)
engine = create_engine("oracle://", execute_sql, strategy="mock")


metadata = MetaData()
Base = declarative_base(metadata=metadata)
class Foo(Base):
    __tablename__ = 'foo'
    name = Column(String(10), primary_key=True)
    __table_args__ = {
        'info': { 
            'oracle_partition': """
                 PARTITION BY HASH(name)
                 ( PARTITION p1 TABLESPACE tbs1
                 , PARTITION p2 TABLESPACE tbs2
                 , PARTITION p3 TABLESPACE tbs3
                 , PARTITION p4 TABLESPACE tbs4
                 )
             """
        }
    }

Foo.__table__.create(bind=engine)

Menggunakan klasik:

m = MetaData()
t = Table(
    'sales_hash', m,
    Column('s_productid', NUMBER),
    Column('s_saledate', DATE),
    Column('s_custid', NUMBER),
    Column('s_totalprice', NUMBER),
    info={
     "oracle_partition": """
         PARTITION BY HASH(s_productid)
         ( PARTITION p1 TABLESPACE tbs1
         , PARTITION p2 TABLESPACE tbs2
         , PARTITION p3 TABLESPACE tbs3
         , PARTITION p4 TABLESPACE tbs4
         )
     """
    }
)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. ORACLE SQL:Isi tanggal yang hilang

  2. java.util.Timestamp.after() salah saat membandingkan milidetik?

  3. Oracle:menyatukan semua kueri 1 dan kueri 2 ingin mengurangi beberapa baris jika kueri 1 memiliki data baris

  4. Oracle SQl Dev, cara menghitung jumlah hari kerja antara 2 tanggal

  5. Oracle setara dengan ROWLOCK, UPDLOCK, petunjuk permintaan READPAST