ddl

SQL基础-----DDL

一个人想着一个人 提交于 2020-01-01 07:19:58
1 (My)SQL入门    这里用了(My)SQL这样的标题,目的是介绍标准SQL的同时,也将MySQL在标准SQL上的扩展一同介绍给读者。 2: SQL分类   SQL语句主要可以划分为以下3个类别    . DDL(Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象。常用的语句关键字主要包括create、drop、alter等。    . DML(Data Manipulation Languages)语句:数据操纵语句,用于添加、删除、更新和查询数据库记录,并检查数据完整性。常用的语句关键字主要包括insert、delete、update和select等。    . DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句,这些语句定义了数据库、表、字段、用户的访问权限和安全级别,主要的语句关键字包括grant、revoke等。 3:DDL语句   DDL是数据定于语言的缩写,简单来说,就是对数据库内部的对象进行创建、删除、修改等操作的语言。它和DML语言的最大区别是DML只是对表内部数据操作,而不涉及表的定义,结构的修改,更不会涉及其他对象。DDL语句更多的由数据库管理员(DBA)使用,开发人员一般很少使用。  

Is string or int preferred for foreign keys?

泄露秘密 提交于 2019-12-31 21:42:13
问题 I have a user table with userid and username columns, and both are unique. Between userid and username , which would be better to use as a foreign key and why? My Boss wants to use string, is that ok? 回答1: It looks like you have both a surrogate key ( int userId ) and a natural key ( char or varchar username ). Either column can be used as a Primary key for the table, and either way, you will still be able to enforce uniqueness of the other key. There are many existing discussions on the

Table cannot be created in mysql -Error 1064

倾然丶 夕夏残阳落幕 提交于 2019-12-31 04:05:06
问题 I am trying to create a table in MySQL with the query CREATE TABLE ofRosterGroups ( rosterID BIGINT NOT NULL, rank TINYINT NOT NULL, groupName VARCHAR(255) NOT NULL, PRIMARY KEY (rosterID, rank), INDEX ofRosterGroup_rosterid_idx (rosterID) ); but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it. Error coming up is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to

How to add a calculated column to Access via SQL

本小妞迷上赌 提交于 2019-12-31 01:45:11
问题 How do i add a calculated column to an Access table in SQL? I know i can add a column with SQL like this: ALTER TABLE Clients ADD COLUMN AccountDate TEXT(60) Thanks, Vítor 回答1: You cannot add a calculated column with SQL because calculated field requires an expression and that cannot be supplied through SQL. Technically, a calculated field is a base type - int, double, text etc. Above the base type is an expression that helps Access do the math/logic. You could use VBA to create a calculated

CREATE UNIQUE INDEX IF NOT EXISTS in postgreSQL

坚强是说给别人听的谎言 提交于 2019-12-30 06:18:08
问题 Plese I would like to do in PostgreSQL something like CREATE UNIQUE INDEX IF NOT EXISTS Any idea? 回答1: You can check, if an index with a given name does exist by this statement. If your index name is some_table_some_field_idx SELECT count(*) > 0 FROM pg_class c WHERE c.relname = 'some_table_some_field_idx' AND c.relkind = 'i'; Starting from Postgres 9.5 you can even use CREATE INDEX IF NOT EXISTS 回答2: Just another ready-to-use solution. PostgreSQL v9.0+: DO $BLOCK$ BEGIN BEGIN CREATE INDEX

How to delete a column from a table in MySQL

时光怂恿深爱的人放手 提交于 2019-12-29 10:03:55
问题 Given the table created using: CREATE TABLE tbl_Country ( CountryId INT NOT NULL AUTO_INCREMENT, IsDeleted bit, PRIMARY KEY (CountryId) ) How can I delete the column IsDeleted ? 回答1: ALTER TABLE tbl_Country DROP COLUMN IsDeleted; Here's a working example. Note that the COLUMN keyword is optional, as MySQL will accept just DROP IsDeleted . Also, to drop multiple columns, you have to separate them by commas and include the DROP for each one. ALTER TABLE tbl_Country DROP COLUMN IsDeleted, DROP

.NET / Oracle: How to execute a script with DDL statements programmatically

淺唱寂寞╮ 提交于 2019-12-29 07:46:07
问题 I want to do some programmatical schema manipulation against an oracle database in C#. Therefore, I struggle with some basic issues. The ddl sql statements are located in a script file. I do not want to use sqlplus.exe, but I want to use OracleCommand out of the ODP.NET assemblies (System.Oracle.DataAccess). Here's an example of my script file: script.sql: DROP TABLE ABCDEF; DROP TABLE GHIJKL; I want to point out: The script contains DDL statements (data definition language) The script

How do I use CREATE OR REPLACE?

ⅰ亾dé卋堺 提交于 2019-12-28 08:06:08
问题 Am I correct in understanding that CREATE OR REPLACE basically means "if the object exists, drop it, then create it either way?" If so, what am I doing wrong? This works: CREATE TABLE foo (id NUMBER, title VARCHAR2(4000) DEFAULT 'Default Title') And this doesn't (ORA-00922: missing or invalid option): CREATE OR REPLACE TABLE foo (id NUMBER, title VARCHAR2(4000) DEFAULT 'Default Title') Am I doing something stupid? I don't seem to be able to find much documentation about this syntax. 回答1: This

直击备份恢复的痛点:基于 TiDB Binlog 的快速时间点恢复

房东的猫 提交于 2019-12-28 06:38:36
作者介绍:吕磊,Better 队成员、美团点评高级 DBA,Better 队参加了 TiDB Hackathon 2019,其项目「基于 TiDB Binlog 的 Fast-PITR」获得了最佳贡献奖。 维护过数据库的同学应该都能体会,数据备份对于数据库来说可以说至关重要,尤其是关键业务。TiDB 原生的备份恢复方案已经在多家客户得到稳定运行的验证,但是对于业务量巨大的系统存在如下几个痛点: 集群中数据量很大的情况下,很难频繁做全量备份。 传统 TiDB Binlog 原样吐出 binlog 增量备份会消耗大量的磁盘空间,并且重放大量 binlog 需要较长时间。 binlog 本身是有向前依赖关系的,任何一个时间点的 binlog 丢失,都会导致后面的数据无法自动恢复。 调大 TiDB gc_life_time 保存更多版本的快照数据,一方面保存时间不能无限长,另一方面过多的版本会影响性能且占用集群空间。 图 1 原生 binlog 备份恢复 我们在线上使用 TiDB 已经超过 2 年,从 1.0 RC 版本到 1.0 正式版、2.0、2.1 以及现在的 3.0,我们能感受到 TiDB 的飞速进步和性能提升,但备份恢复的这些痛点,是我们 TiDB 在关键业务中推广的一个掣肘因素。于是,我们选择了这个题目: 基于 TiDB Binlog 的 Fast-PITR (Fast

第四章-DML,DDL和DCL语言

时光怂恿深爱的人放手 提交于 2019-12-28 04:03:01
DML语言 数据的插入 单行插入 insert into table1 (列名) values (列值); insert into table1 (列名) set (列值); 多行插入 inset into table1 (列名) values (列值),(列值); 注: 列名和列值的数量要相同而且一 一对应,不能为null的列都要给值 列名可以隐去,这样相当所有列都赋值,如果可以为null要显性给null值,那么可以使用NULL 对于字符好日期型,值要给单引号,数值则不用 数据的修改 单个表 update table1 set 列名a = 列值a,列名b = 列值b where 条件列式; 多表 (一定要连接条件,不然你的条件列式如果只关于一个表只能限制一个表,那么另一个表可能会成为全表修改,如果用and则表示要两个条件成立,会修改) 其实多表修改,其实就相当于先查询再修改,没有连接条件,就不会产生两个表连接的临时表,那么如果对两个表都有条件则找不到数据,不能修改 update table1 as a,table2 as b set a.列名a=列值a1,a.列名b = 列值a2 where 连接条件 and 条件列式 数据的删除 单个表 delete from table where 条件列式 多表 delete a,b from table1 as a,table2 as b