试图完成一些功课并遇到创建表格的问题.如何声明一系列数字的列默认值.它的内容是:"列构建(默认为1但可以是1-10)"我似乎找不到......或者知道在哪里查找这些信息.
CREATE TABLE tblDepartment
(
Department_ID int NOT NULL IDENTITY,
Department_Name varchar(255) NOT NULL,
Division_Name varchar(255) NOT NULL,
City varchar(255) default 'spokane' NOT NULL,
Building int default 1 NOT NULL,
Phone varchar(255)
)
Run Code Online (Sandbox Code Playgroud)
我试过建立int默认1在1到10之间NOT NULL,这没有用,我试过建立int默认1-10,表创建但我不认为它是正确的.
是否可以在SQLAlchemy中创建没有主键的表?我想要定义的关系如下:
class TPost(Base):
__tablename__ = "forum_post"
id = Column(Integer, primary_key = True)
topic_id = Column(Integer, ForeignKey("forum_topic.id"))
index = Column(Integer)
page = Column(Integer)
user_id = Column(Integer, ForeignKey("forum_user.id"))
posted_at = Column(DateTime)
post_text = Column(String)
has_quotes = Column(Boolean)
quotes = relationship("TQuote")
class TQuote(Base):
__tablename__ = "forum_quotes"
id = Column(Integer, ForeignKey("forum_post.id"))
is_direct = Column(Boolean)
quoted_text = Column(String)
quoted_id = Column(Integer)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我并不需要主键,我不打算Quote将来扩展这种关系.
我的问题具体由此错误消息表示:
sqlalchemy.exc.ArgumentError: Mapper Mapper|TQuote|forum_quotes
could not assemble any primary key columns for mapped table 'forum_quotes'
Run Code Online (Sandbox Code Playgroud)
编辑:
该(id,quoted_id)对是唯一的,它存在于大多数数据中,但是当引用不是直接的(并且在这种情况下没有quoted_id)时,我将引用的文本直接内联到引用关系中.我可以使用双表方法(其中直接引号具有带主键的表),但我真的宁愿将其实现为单个一对多关系.我不想做一次以上的加入.
编辑2:
我将对引号进行编号并使用外键+应用程序生成的数字作为pkey,仍然令人烦恼.现在来弄清楚语法. …
我今天开始申请,当它开始时,我得到了这个错误
| 错误2012-09-14 13:54:17,608 [pool-7-thread-1]错误hbm2ddl.SchemaExport - 不成功:创建表顺序(默认情况下生成的id bigint为identity,version bigint not null,date_created timestamp not null,order varchar(255)not null,picture_id bigint not null,posts_id bigint not null,主键(id))
| 错误2012-09-14 13:54:17,609 [pool-7-thread-1]错误hbm2ddl.SchemaExport - SQL语句中的语法错误"CREATE TABLE ORDER [*](ID BIGINT由DEFAULT生成IDENTITY,VERSION BIGINT NOT NULL ,DATE_CREATED TIMESTAMP NOT NULL,ORDER VARCHAR(255)NOT NULL,PICTURE_ID BIGINT NOT NULL,POSTS_ID BIGINT NOT NULL,PRIMARY KEY(ID))"; 预期的"标识符"; SQL语句:创建表顺序(默认情况下生成的id bigint为identity,version bigint not null,date_created timestamp not null,order varchar(255)not null,picture_id bigint not null,posts_id bigint not null,primary key(id))[ 42001-164]
| 错误2012-09-14 13:54:17,621 [pool-7-thread-1]错误hbm2ddl.SchemaExport - 不成功:alter table order add constraint FK651874E9A8021F6 foreign key(posts_id)references …
有任何想法,如何添加列:
ALTER TABLE tname ADD COLUMN cname TEXT CHARACTER SET utf8 COLLATE utf8_general_ci IF NOT EXISTS;
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误:
ALTER TABLE tname ADD COLUMN cname TEXT CHARACTER SET utf8 COLLATE utf8_general_ci IF NOT EXISTS;
Run Code Online (Sandbox Code Playgroud)
我需要使用utf-8编码添加新列.
谢谢
我正在尝试向 firebird 表添加一列。该列应该有一个默认值 1,但最初对于已经存在的行,该值应该设置为 0。这应该发生在一个事务中。
我试过
ALTER TABLE MYTABLE ADD MYCOLUMN SMALLINT DEFAULT 1 NOT NULL;
UPDATE MYTABLE SET MYCOLUMN = 0;
Run Code Online (Sandbox Code Playgroud)
但这在一笔交易中是不允许的,因为更新不会看到新列。我也试过:
ALTER TABLE MYTABLE ADD MYCOLUMN SMALLINT DEFAULT 0 NOT NULL;
ALTER TABLE MYTABLE ALTER COLUMN MYCOLUMN SET DEFAULT 1 NOT NULL;
Run Code Online (Sandbox Code Playgroud)
希望该列之后为 0 但它将为 1。
在一笔交易中获得我想要的东西的更多选择?
所以我在创建脚本时一直盲目地使用ansi_nulls on、quoted_identifier on,因为sqlserver在编写对象脚本时会自动生成它们。
我真的没有时间关心这些琐碎的废话:-)但我想必须提出这些问题。
鉴于这些是推荐的设置,是否有一个选项可以设置(a)每个数据库和(b)每个服务器,以便它们默认情况下始终处于打开状态,而不是通过单独的脚本?
如果是这样,如何/在哪里可以查看当前的默认设置?
迁移到Oracle 18c企业版后,无法创建基于函数的索引。
这是我的索引 DDL:
CREATE INDEX my_index ON my_table
(UPPER( REGEXP_REPLACE ("DEPT_NUM",'[^[:alnum:]]',NULL,1,0)))
TABLESPACE my_tbspace
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ORA-01743: only pure functions can be indexed
01743. 00000 - "only pure functions can be indexed"
*Cause: The indexed function uses SYSDATE or the user environment.
*Action: PL/SQL functions must be pure (RNDS, RNPS, WNDS, WNPS). SQL
expressions must not use SYSDATE, USER, USERENV(), or anything …Run Code Online (Sandbox Code Playgroud) 我试图用 SQLAlchemy 描述遗留 SQL 模式中存在的关系。我将架构翻译为:
class Lang(Base):
__tablename__ = "tblLang"
id = Column("ID", Integer, primary_key=True)
# ...
class ItemName(Base):
__tablename__ = "tblItemName"
# ItemName has a composite primary key
id = Column("ID", Integer, primary_key=True)
lang_id = Column("Lang", Integer, ForeignKey("Lang.id"), primary_key=True)
name = Column("Name", String)
class Item(Base):
__tablename__ = "tblItem"
id = Column("id", Integer, primary_key=True)
inventory = Column("Inventory", Integer)
# item_name_id specifies only part of the table
# relationship
item_name_id = Column("ItemNameID", Integer, ForeignKey("tblItemName.ID"))
# lang_id should come from outside the …Run Code Online (Sandbox Code Playgroud) MySQL 8.0 - 正如MySQL 官方文档和MySQL 服务器团队中所述,Alter table with Algorithm=INSTANT 会立即添加列,而不需要任何锁定。
但是,结果不同了。
测试设置- 具有 40M 行的表。在表上读取和写入 (1000 TPS)。表架构非常简单。
field,type,null,key,default,extra
id,bigint(20),NO,PRI,,auto_increment
t_a,bigint(20),NO,MUL,,""
t_b,bigint(20),NO,MUL,,""
t_c,int(11),NO,"",1,""
Run Code Online (Sandbox Code Playgroud)
基础设施: AWS RDS MySQL 8 - 引擎版本 - 8.0.17
测试用例: 向表中添加新列。
陈述:
alter table table_name add column_name int, algorithm=instant;
Run Code Online (Sandbox Code Playgroud)
结果:
mysql> alter table test_table add test_column int, algorithm=instant;
Query OK, 0 rows affected (36.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
Run Code Online (Sandbox Code Playgroud)
花了大约 40 秒。它在40 秒内阻止了读取和写入
这是预期的吗?我有什么遗漏的吗?
有没有一种方法可以not null在一个语句中向列添加约束并用默认值替换所有现有的空值?
alter table t
alter column c set default 0,
alter column c set not null;
Run Code Online (Sandbox Code Playgroud)
似乎不起作用,给出错误:
Run Code Online (Sandbox Code Playgroud)column "c" contains null values
ddl ×10
sql ×4
mysql ×2
python ×2
sqlalchemy ×2
alter ×1
alter-table ×1
amazon-rds ×1
database ×1
firebird ×1
firebird2.5 ×1
foreign-keys ×1
grails ×1
indexing ×1
innodb ×1
oracle ×1
oracle18c ×1
orm ×1
postgresql ×1
regex ×1
sql-scripts ×1
sql-server ×1