pub struct TableCreateStatement { /* private fields */ }Expand description
Create a table
ยงExamples
use sea_query::{*, tests_cfg::*};
let table = Table::create()
.table(Char::Table)
.if_not_exists()
.comment("table's comment")
.col(ColumnDef::new(Char::Id).integer().not_null().auto_increment().primary_key())
.col(ColumnDef::new(Char::FontSize).integer().not_null().comment("font's size"))
.col(ColumnDef::new(Char::Character).string().not_null())
.col(ColumnDef::new(Char::SizeW).integer().not_null())
.col(ColumnDef::new(Char::SizeH).integer().not_null())
.col(ColumnDef::new(Char::FontId).integer().default(Value::Int(None)))
.foreign_key(
ForeignKey::create()
.name("FK_2e303c3a712662f1fc2a4d0aad6")
.from(Char::Table, Char::FontId)
.to(Font::Table, Font::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
[
r#"CREATE TABLE IF NOT EXISTS `character` ("#,
r#"`id` int NOT NULL PRIMARY KEY AUTO_INCREMENT,"#,
r#"`font_size` int NOT NULL COMMENT 'font\'s size',"#,
r#"`character` varchar(255) NOT NULL,"#,
r#"`size_w` int NOT NULL,"#,
r#"`size_h` int NOT NULL,"#,
r#"`font_id` int DEFAULT NULL,"#,
r#"CONSTRAINT `FK_2e303c3a712662f1fc2a4d0aad6`"#,
r#"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`)"#,
r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
r#") COMMENT 'table\'s comment'"#,
].join(" ")
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" varchar NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
r#"CONSTRAINT "FK_2e303c3a712662f1fc2a4d0aad6""#,
r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id")"#,
r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
r#")"#,
].join(" ")
);
assert_eq!(
table.to_string(SqliteQueryBuilder),
[
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" varchar NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id") ON DELETE CASCADE ON UPDATE CASCADE"#,
r#")"#,
].join(" ")
);Implementationsยง
Sourceยงimpl TableCreateStatement
impl TableCreateStatement
Sourcepub fn if_not_exists(&mut self) -> &mut Self
pub fn if_not_exists(&mut self) -> &mut Self
Create table if table not exists
Sourcepub fn table<T>(&mut self, table: T) -> &mut Selfwhere
T: IntoTableRef,
pub fn table<T>(&mut self, table: T) -> &mut Selfwhere
T: IntoTableRef,
Set table name
Sourcepub fn col<C: IntoColumnDef>(&mut self, column: C) -> &mut Self
pub fn col<C: IntoColumnDef>(&mut self, column: C) -> &mut Self
Add a new table column
pub fn check<T>(&mut self, value: T) -> &mut Self
Sourcepub fn index(&mut self, index: &mut IndexCreateStatement) -> &mut Self
pub fn index(&mut self, index: &mut IndexCreateStatement) -> &mut Self
Add an index. MySQL only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.index(Index::create().unique().name("idx-glyph-id").col(Glyph::Id))
.to_string(MysqlQueryBuilder),
[
"CREATE TABLE `glyph` (",
"`id` int NOT NULL,",
"UNIQUE KEY `idx-glyph-id` (`id`)",
")",
]
.join(" ")
);Sourcepub fn primary_key(&mut self, index: &mut IndexCreateStatement) -> &mut Self
pub fn primary_key(&mut self, index: &mut IndexCreateStatement) -> &mut Self
Add an primary key.
ยงExamples
use sea_query::{tests_cfg::*, *};
let mut statement = Table::create();
statement
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.col(ColumnDef::new(Glyph::Image).string().not_null())
.primary_key(Index::create().col(Glyph::Id).col(Glyph::Image));
assert_eq!(
statement.to_string(MysqlQueryBuilder),
[
"CREATE TABLE `glyph` (",
"`id` int NOT NULL,",
"`image` varchar(255) NOT NULL,",
"PRIMARY KEY (`id`, `image`)",
")",
]
.join(" ")
);
assert_eq!(
statement.to_string(PostgresQueryBuilder),
[
"CREATE TABLE \"glyph\" (",
"\"id\" integer NOT NULL,",
"\"image\" varchar NOT NULL,",
"PRIMARY KEY (\"id\", \"image\")",
")",
]
.join(" ")
);
assert_eq!(
statement.to_string(SqliteQueryBuilder),
[
r#"CREATE TABLE "glyph" ("#,
r#""id" integer NOT NULL,"#,
r#""image" varchar NOT NULL,"#,
r#"PRIMARY KEY ("id", "image")"#,
r#")"#,
]
.join(" ")
);Sourcepub fn foreign_key(
&mut self,
foreign_key: &mut ForeignKeyCreateStatement,
) -> &mut Self
pub fn foreign_key( &mut self, foreign_key: &mut ForeignKeyCreateStatement, ) -> &mut Self
Add a foreign key
Sourcepub fn character_set<T>(&mut self, name: T) -> &mut Self
pub fn character_set<T>(&mut self, name: T) -> &mut Self
Set database character set. MySQL only.
Sourcepub fn partition_by_range<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
pub fn partition_by_range<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
Set partition by range. Postgres and MySQL only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.partition_by_range([Glyph::Id])
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph" ( "id" integer NOT NULL ) PARTITION BY RANGE ("id")"#
);Sourcepub fn partition_by_list<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
pub fn partition_by_list<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
Set partition by list. Postgres and MySQL only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.partition_by_list([Glyph::Id])
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph" ( "id" integer NOT NULL ) PARTITION BY LIST ("id")"#
);Sourcepub fn partition_by_hash<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
pub fn partition_by_hash<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
Set partition by hash. Postgres and MySQL only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.partition_by_hash([Glyph::Id])
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph" ( "id" integer NOT NULL ) PARTITION BY HASH ("id")"#
);Sourcepub fn partition_by_key<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
pub fn partition_by_key<I, T>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: IntoIden,
Set partition by key. MySQL only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.partition_by_key([Glyph::Id])
.to_string(MysqlQueryBuilder),
"CREATE TABLE `glyph` ( `id` int NOT NULL ) PARTITION BY KEY (`id`)"
);Sourcepub fn partition_of<T>(&mut self, table: T) -> &mut Selfwhere
T: IntoTableRef,
pub fn partition_of<T>(&mut self, table: T) -> &mut Selfwhere
T: IntoTableRef,
Set partition of table. Postgres only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Alias::new("glyph_1"))
.partition_of(Glyph::Table)
.values_from_to([1], [10])
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph_1" PARTITION OF "glyph" FOR VALUES FROM (1) TO (10)"#
);Sourcepub fn values_in<I, T>(&mut self, values: I) -> &mut Self
pub fn values_in<I, T>(&mut self, values: I) -> &mut Self
Set partition values IN. Postgres partition tables only.
MySQL partition definitions can use PartitionValues::In with
Self::add_partition.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Alias::new("glyph_1"))
.partition_of(Glyph::Table)
.values_in([1, 2, 3])
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph_1" PARTITION OF "glyph" FOR VALUES IN (1, 2, 3)"#
);Sourcepub fn values_from_to<I, T, J, U>(&mut self, from: I, to: J) -> &mut Self
pub fn values_from_to<I, T, J, U>(&mut self, from: I, to: J) -> &mut Self
Set partition values FROM โฆ TO โฆ. Postgres only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Alias::new("glyph_1"))
.partition_of(Glyph::Table)
.values_from_to([1], [10])
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph_1" PARTITION OF "glyph" FOR VALUES FROM (1) TO (10)"#
);Sourcepub fn values_less_than<I, T>(&mut self, values: I) -> &mut Self
pub fn values_less_than<I, T>(&mut self, values: I) -> &mut Self
Set partition values LESS THAN. MySQL partition definitions only.
Use PartitionValues::LessThan with Self::add_partition for MySQL
table partition definitions.
Sourcepub fn values_with(&mut self, modulus: u32, remainder: u32) -> &mut Self
pub fn values_with(&mut self, modulus: u32, remainder: u32) -> &mut Self
Set partition values WITH (modulus, remainder). Postgres only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Alias::new("glyph_p1"))
.partition_of(Glyph::Table)
.values_with(4, 0)
.to_string(PostgresQueryBuilder),
r#"CREATE TABLE "glyph_p1" PARTITION OF "glyph" FOR VALUES WITH (MODULUS 4, REMAINDER 0)"#
);Sourcepub fn add_partition<T>(
&mut self,
name: T,
values: Option<PartitionValues>,
) -> &mut Selfwhere
T: IntoIden,
pub fn add_partition<T>(
&mut self,
name: T,
values: Option<PartitionValues>,
) -> &mut Selfwhere
T: IntoIden,
Add a partition definition. MySQL only.
ยงExamples
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(ColumnDef::new(Glyph::Id).integer().not_null())
.partition_by_range([Glyph::Id])
.add_partition(
Alias::new("p0"),
Some(PartitionValues::LessThan(vec![10.into()]))
)
.to_string(MysqlQueryBuilder),
"CREATE TABLE `glyph` ( `id` int NOT NULL ) PARTITION BY RANGE (`id`) ( PARTITION `p0` VALUES LESS THAN (10) )"
);pub fn get_table_name(&self) -> Option<&TableRef>
pub fn get_columns(&self) -> &Vec<ColumnDef>
pub fn get_comment(&self) -> Option<&String>
pub fn get_foreign_key_create_stmts(&self) -> &Vec<ForeignKeyCreateStatement>
pub fn get_indexes(&self) -> &Vec<IndexCreateStatement>
Sourcepub fn extra<T>(&mut self, extra: T) -> &mut Self
pub fn extra<T>(&mut self, extra: T) -> &mut Self
Rewriting extra param. You should take care self about concat extra params. Add extra after options. Example for PostgresSQL Citus extension:
use sea_query::{tests_cfg::*, *};
let table = Table::create()
.table(Char::Table)
.col(
ColumnDef::new(Char::Id)
.uuid()
.extra("DEFAULT uuid_generate_v4()")
.primary_key()
.not_null(),
)
.col(
ColumnDef::new(Char::CreatedAt)
.timestamp_with_time_zone()
.extra("DEFAULT NOW()")
.not_null(),
)
.col(ColumnDef::new(Char::UserData).json_binary().not_null())
.extra("USING columnar")
.take();
assert_eq!(
table.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE "character" ("#,
r#""id" uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),"#,
r#""created_at" timestamp with time zone NOT NULL DEFAULT NOW(),"#,
r#""user_data" jsonb NOT NULL"#,
r#") USING columnar"#,
]
.join(" ")
);pub fn get_extra(&self) -> Option<&String>
Sourcepub fn temporary(&mut self) -> &mut Self
pub fn temporary(&mut self) -> &mut Self
Create temporary table
Ref:
- PostgreSQL: https://www.postgresql.org/docs/17/sql-createtable.html#SQL-CREATETABLE-TEMPORARY
- MySQL: https://dev.mysql.com/doc/refman/9.2/en/create-temporary-table.html
- MariaDB: https://mariadb.com/kb/en/create-table/#create-temporary-table
- SQLite: https://sqlite.org/lang_createtable.html
ยงExamples
use sea_query::{tests_cfg::*, *};
let statement = Table::create()
.table(Font::Table)
.temporary()
.col(
ColumnDef::new(Font::Id)
.integer()
.not_null()
.primary_key()
.auto_increment(),
)
.col(ColumnDef::new(Font::Name).string().not_null())
.take();
assert_eq!(
statement.to_string(MysqlQueryBuilder),
[
"CREATE TEMPORARY TABLE `font` (",
"`id` int NOT NULL PRIMARY KEY AUTO_INCREMENT,",
"`name` varchar(255) NOT NULL",
")",
]
.join(" ")
);
assert_eq!(
statement.to_string(PostgresQueryBuilder),
[
r#"CREATE TEMPORARY TABLE "font" ("#,
r#""id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,"#,
r#""name" varchar NOT NULL"#,
r#")"#,
]
.join(" ")
);
assert_eq!(
statement.to_string(SqliteQueryBuilder),
[
r#"CREATE TEMPORARY TABLE "font" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""name" varchar NOT NULL"#,
r#")"#,
]
.join(" ")
);pub fn take(&mut self) -> Self
Sourceยงimpl TableCreateStatement
impl TableCreateStatement
pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String
pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String
Trait Implementationsยง
Sourceยงimpl Clone for TableCreateStatement
impl Clone for TableCreateStatement
Sourceยงfn clone(&self) -> TableCreateStatement
fn clone(&self) -> TableCreateStatement
1.0.0 (const: unstable) ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more