![]() For example, to create the CITIES table, the change will look like this: Īll three columns are marked as primary keys with the same primary key name. It is just enough to add the constraints tag with the primary key attributes to all columns, we want to create the key on. It is unobvious how to deal with a primary key built on multiple columns like in the CITIES table from the beginning of the article. The above XML changeset is an equivalent of this SQL statement in MySQL: CREATE TABLE USERS ( It is true by default, so if you do not want to allow null values in the column, set it to false.Īs Liquibase changes support multiple database engines, there are also attributes specific to Oracle like primaryKeyTablespace, which allows to define on which tablespace an index supporting the primary key will be created. nullable - it is a flag ( true or false).It is a totally new name that will be assigned to the newly created primary key. It will be easier to manage the constraint in the future. I highly recommend always setting it to a custom value. primaryKeyName - it is an optional attribute, but very useful.When set to true, the column is marked as a primary key. primaryKey - it is a flag ( true or false).This example has three elements that are most commonly used when defining a primary key: As you can see, the primary key is defined by a nested XML tag - constraints. One of them (US_ID) has an additional constraint - a primary key. The createTable change creates a new table with two columns. Using a Liquibase changeset in XML format, it may look like this: So when we are ready, the table is created at once with columns and a primary key. Usually, when designing a table, we define a primary key. In short, it might be described like that: primary key on CITIES (CI_COUNTRY, CI_POSTAL_CODE, CI_NAME)Ĭreate table with primary key Single column PK Only all three: country, postal code and name identify a row in the table. There is no single column that guarantees uniqueness. A good example might be a table with cities. It is a primary key, but is created on more than one column. In the above example, the primary key may be described like that: primary key on USERS (US_ID)Īnother example is a composite primary key. For example, it may be automatically generated when a new row is inserted. An obvious candidate for a primary key is a single column US_ID, assuming it is unique in the whole table. How to create a table with a primary key? How to add or modify a primary key later when a table already exists?Ī primary key is a constraint on a table that defines a set of columns, whose values uniquely identify a row in the table.Ī simple example is a table with users, that stores their id, first name and last name. Even though tables are very common, creating them isn't the most straightforward operation you can think of. There is no useful relational database without tables. Or a phrase commonly used would be better. Creating a table is one of the most basic elements of Data Definition Language (DDL).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |