Questions tagged [ddl]

Data Definition Language is a subset of SQL to manipulate structural elements of a database, not the content of tables. CREATE, DROP, ALTER and related statements.

Data Definition Language is a subset of SQL to manipulate structural elements of a database, not the content of tables. CREATE, DROP, ALTER and related statements. It is often restricted to database users with administrative privileges.

Related tags

2245 questions
739
votes
9 answers

SQLite primary key on multiple columns

What is the syntax for specifying a primary key on more than 1 column in SQLite ?
Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74
713
votes
34 answers

How to reset Postgres' primary key sequence when it falls out of sync?

I ran into the problem that my primary key sequence is not in sync with my table rows. That is, when I insert a new row I get a duplicate key error because the sequence implied in the serial datatype returns a number that already exists. It seems…
meleyal
  • 32,252
  • 24
  • 73
  • 79
510
votes
9 answers

How to delete a column from a table in MySQL

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?
raji
  • 5,265
  • 2
  • 13
  • 4
502
votes
13 answers

What are DDL and DML?

I have heard the terms DDL and DML in reference to databases, but I don't understand what they are. What are they and how do they relate to SQL?
Sachindra
  • 6,421
  • 6
  • 29
  • 38
479
votes
11 answers

Adding multiple columns AFTER a specific column in MySQL

I need to add multiple columns to a table but position the columns after a column called lastname. I have tried this: ALTER TABLE `users` ADD COLUMN ( `count` smallint(6) NOT NULL, `log` varchar(12) NOT NULL, `status` int(10) unsigned…
Koala
  • 5,253
  • 4
  • 25
  • 34
334
votes
8 answers

Postgres manually alter sequence

I'm trying to set a sequence to a specific value. SELECT setval('payments_id_seq'), 21, true; This gives an error: ERROR: function setval(unknown) does not exist Using ALTER SEQUENCE doesn't seem to work either? ALTER SEQUENCE payments_id_seq…
stef
  • 26,771
  • 31
  • 105
  • 143
312
votes
8 answers

PostgreSQL: Give all permissions to a user on a PostgreSQL database

I would like to give a user all the permissions on a database without making it an admin. The reason why I want to do that is that at the moment DEV and PROD are different DBs on the same cluster so I don't want a user to be able to change…
Diego
  • 34,802
  • 21
  • 91
  • 134
294
votes
6 answers

PostgreSQL create table if not exists

In a MySQL script you can write: CREATE TABLE IF NOT EXISTS foo ...; ... other stuff ... and then you can run the script many times without re-creating the table. How do you do this in PostgreSQL?
peter2108
  • 5,580
  • 6
  • 24
  • 18
291
votes
5 answers

How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?

I was working on my Spring boot app project and noticed that, sometimes there is a connection time out error to my Database on another server(SQL Server). This happens specially when I try to do some script migration with FlyWay but it works after…
METTAIBI
  • 3,201
  • 5
  • 22
  • 29
216
votes
11 answers

Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?

I want to create a database which does not exist through JDBC. Unlike MySQL, PostgreSQL does not support create if not exists syntax. What is the best way to accomplish this? The application does not know if the database exists or not. It should…
Aman Deep Gautam
  • 8,091
  • 21
  • 74
  • 130
188
votes
14 answers

How do I add a foreign key to an existing SQLite table?

I have the following table: CREATE TABLE child( id INTEGER PRIMARY KEY, parent_id INTEGER, description TEXT); How do I add a foreign key constraint on parent_id? Assume foreign keys are enabled. Most examples assume you're creating the…
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
167
votes
6 answers

There can be only one auto column

How do I correct the error from MySQL 'you can only have one auto increment column'. CREATE TABLE book ( id INT AUTO_INCREMENT NOT NULL, accepted_terms BIT(1) NOT NULL, accepted_privacy BIT(1) NOT NULL ) ENGINE=InnoDB DEFAULT…
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
154
votes
4 answers

Alter Table Add Column Syntax

I'm trying to programmatically add an identity column to a table Employees. Not sure what I'm doing wrong with my syntax. ALTER TABLE Employees ADD COLUMN EmployeeID int NOT NULL IDENTITY (1, 1) ALTER TABLE Employees ADD CONSTRAINT …
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
137
votes
11 answers

Drop column from SQLite table

I have a problem: I need to delete a column from my SQLite database. I wrote this query alter table table_name drop column column_name but it does not work. Please help me.
sandy
  • 1,908
  • 3
  • 19
  • 23
134
votes
1 answer

Adding comment to column when I create table in PostgreSQL?

How can I add comment to column in PostgreSQL? create table session_log ( UserId int index not null, PhoneNumber int index);
user3600910
  • 2,839
  • 4
  • 22
  • 36
1
2 3
99 100