Effect of Anonymous Columns
Now, recall that a VALUES expression denotes a table with undefined column names. If an initial value is to be specified when a base table is created, column definitions have to be implied by that initial value, so the question arises, how can a VALUES expression provide the initial value for a base table? The answer is that you have to learn an extra syntactic construct for that purpose, shown in Example.
Example: Specifying an initial value for a base table.
CREATE TABLE ENROLMENT (StudentId, Name, CourseId)
AS (VALUES (SID ('S1'), 'Anne', CID ('C1')),
(SID ('S2'), 'Boris', CID ('C1')))
WITH DATA;
Explanation:
- (StudentId, Name, CourseId) provides the names, positionally corresponding to the anonymous columns of the AS table.
- WITH DATA specifies that the given table is indeed to be the initial value. Curiously, this is required, unless WITHOUT DATA is written instead, in which case the AS table serves only to determine the declared types of the columns of the base table.
- SQL does not allow constraints to be declared if AS is used in place of explicit column definitions.