在SQL中,references关键字用于在创建表时定义外键约束。外键约束用于确保一个表中的数据与另一个表中的数据之间的关系的完整性。
语法如下:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
FOREIGN KEY (column1) REFERENCES other_table (other_column)
);
在上面的语法中,table_name是要创建的表的名称,column1是要定义为外键的列名,datatype是该列的数据类型,other_table是引用的表的名称,other_column是引用表中的列名。
例如,如果我们有一个orders表和一个customers表,我们希望orders表中的customer_id列引用customers表中的customer_id列,可以这样定义外键约束:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
这将创建一个orders表,其中customer_id列是外键,它引用customers表中的customer_id列。