sql中外键怎么写(sql中外键怎么写)
sql中外键怎么写的方法。如下参考:1.创建测试表;创建表test_class(class_idvarchar2(10),class_(30));创建表test_student(student_idvarchar2(10),student_(30),class_idvarchar2(10));2.表test_class创建主键,test_student添加外键;创建/重新创建_CLASS_class_(CLASS_ID);创建/重新创建_STUDENT_class_(CLASS_ID)_class(CLASS_ID);3.主键在表中,插入数据;_classvalues(1001,’class1’);_classvalues(1002,’class2’);_classvalues(1003,’class3’);提交;4.在外键表中插入数据,但class_id没有在主键表中定义,可以查找错误信息;InsertintoTEST_STUDENTvalues(100001,’kingtwo’,1004);5.如果将数据插入外键表,并且在主键表中定义了class_id,则可以正常插入;InsertintoTEST_STUDENTvalues(100001,’kingtwo’,1001);InsertintoTEST_STUDENTvalues(100002,’kingtwo’,1002);InsertintoTEST_STUDENTvalues(100003,’twoKings’,1003);提交;
sql中外键怎么写?
sql中外键怎么写的方法。如下参考:1.创建测试表;创建表test_class(class_idvarchar2(10),class_namevarchar2(30));创建表test_student(student_idvarchar2(10),student_namevarchar2(30),class_idvarchar2(10));2.表test_class创建主键,test_student添加外键;创建/重新创建eprimaryuniqueandforeignkeyconstraintsaltertableTEST_CLASSAddconstraintp_class_idprimarykey(CLASS_ID);创建/重新创建eprimaryuniqueandforeignkeyconstraintsaltertableTEST_STUDENTAddconstraintf_class_idforeignkey(CLASS_ID)Referencestest_class(CLASS_ID)ondeletecascade;3.主键在表7a686964616fe58685e5aeb931333433623133中,插入数据;Insertintotest_classvalues(1001,'class1');Insertintotest_classvalues(1002,'class2');Insertintotest_classvalues(1003,'class3');提交;4.在外键表中插入数据,但class_id没有在主键表中定义,可以查找错误信息;InsertintoTEST_STUDENTvalues(100001,'kingtwo',1004);5.如果将数据插入外键表,并且在主键表中定义了class_id,则可以正常插入;InsertintoTEST_STUDENTvalues(100001,'kingtwo',1001);InsertintoTEST_STUDENTvalues(100002,'kingtwo',1002);InsertintoTEST_STUDENTvalues(100003,'twoKings',1003);提交;
外键约束中,以下错误的是()。
外键约束中,以下错误的是()。
A.B表外键列的取值必须是A表主键列或者唯一索引列中的取值。
B.A表主键列或者唯一索引列取值发生改变,B表外键列的值也发生改变。
C.在MySQL中,所有的存储引擎均支持外键约束。
D.A表主键列或者唯一索引列取值删除,B表外键列取值与该值相同的行会被删除。
正确答案:C
Django模型中的OneToOneField和ForeignKey有什么区别
说是ForeignKey是one-to-many的,并举了一个车的例子:
有两个配件表,一个是车轮表,另一个是引擎表。两个表都有一个car字段,表示该配件对应的车。
对于车轮来说,多个对应一个car的情况很正常,所以car字段应该用ForeignKey来表示。
对于引擎来说,一个引擎只可能对应一个car,所以必须用OneToOneField。
OneToOneField(someModel) 可以理解为 ForeignKey(SomeModel, unique=True)。
两者的反向查询是有差别的:
ForeignKey反向查询返回的是一个列表(一个车有多个轮子)。
OneToOneField反向查询返回的是一个模型示例(因为一对一关系)。
另外的补充说明:
Be careful to realize that there are some differences between OneToOneField(SomeModel) andForeignKey(SomeModel, unique=True). As stated in The Definitive Guide to Django:
OneToOneField
A one-to-one relationship. Conceptually, this is similar toa ForeignKey with unique=True, but the "reverse" side of the relation will directly return a single object.
In contrast to the OneToOneField "reverse" relation, a ForeignKey "reverse" relation returns aQuerySet.
Example
For example, if we have the following two models (full model code below):
Car model uses OneToOneField(Engine)
Car2 model uses ForeignKey(Engine2, unique=True)
From within python manage.py shell execute the following:
OneToOneField Example
>>> from testapp.models import Car, Engine
>>> c = Car.objects.get(name='Audi')
>>> e = Engine.objects.get(name='Diesel')
>>> e.car
ForeignKey with unique=True Example
>>> from testapp.models import Car2, Engine2
>>> c2 = Car2.objects.get(name='Mazda')
>>> e2 = Engine2.objects.get(name='Wankel')
>>> e2.car2_set.all()
[]
Model Code
from django.db import models
class Engine(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Car(models.Model):
name = models.CharField(max_length=25)
engine = models.OneToOneField(Engine)
def __unicode__(self):
return self.name
class Engine2(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Car2(models.Model):
name = models.CharField(max_length=25)
engine = models.ForeignKey(Engine2, unique=True)
def __unicode__(self):
return self.name
Django模型中的OneToOneField和ForeignKey有什么区别
官方文档中说明:OneToOneFieldA one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the "reverse" side of the relation will directly return a single object. 我们可以认为当ForeignKey 的unique参数为True时,OneToOneField和ForeignKey 在定义上是类似的,区别在于获取数据时,OneToOneField返回的是一个单独的对象,而ForeignKey 返回的是一个QuerySet model.pyfrom django.db import modelsclass Engine(models.Model): name = models.CharField(max_length=25) def __unicode__(self): return self.nameclass Car(models.Model): name = models.CharField(max_length=25) engine = models.OneToOneField(Engine) def __unicode__(self): return self.nameclass Engine2(models.Model): name = models.CharField(max_length=25) def __unicode__(self): return self.nameclass Car2(models.Model): name = models.CharField(max_length=25) engine = models.ForeignKey(Engine2, unique=True) def __unicode__(self): return self.name 当我们在python manage.py shell中执行时 OneToOneField:>>> from testapp.models import Car, Engine>>> c = Car.objects.get(name='Audi')>>> e = Engine.objects.get(name='Diesel')>>> e.car ForeignKey with unique=True:>>> from testapp.models import Car2, Engine2>>> c2 = Car2.objects.get(name='Mazda')>>> e2 = Engine2.objects.get(name='Wankel')>>> e2.car2_set.all()[] 参考:http://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey
Django模型中的OneToOneField和ForeignKey有什么区别
说是ForeignKey是one-to-many的,并举了一个车的例子:
有两个配件表,一个是车轮表,另一个是引擎表。两个表都有一个car字段,表示该配件对应的车。
对于车轮来说,多个对应一个car的情况很正常,所以car字段应该用ForeignKey来表示。
对于引擎来说,一个引擎只可能对应一个car,所以必须用OneToOneField。
OneToOneField(someModel) 可以理解为 ForeignKey(SomeModel, unique=True)。
两者的反向查询是有差别的:
ForeignKey反向查询返回的是一个列表(一个车有多个轮子)。
OneToOneField反向查询返回的是一个模型示例(因为一对一关系)。
另外的补充说明:
Be careful to realize that there are some differences between OneToOneField(SomeModel) andForeignKey(SomeModel, unique=True). As stated in The Definitive Guide to Django:
OneToOneField
A one-to-one relationship. Conceptually, this is similar toa ForeignKey with unique=True, but the "reverse" side of the relation will directly return a single object.
In contrast to the OneToOneField "reverse" relation, a ForeignKey "reverse" relation returns aQuerySet.
Example
For example, if we have the following two models (full model code below):
Car model uses OneToOneField(Engine)
Car2 model uses ForeignKey(Engine2, unique=True)
From within python manage.py shell execute the following:
OneToOneField Example
>>> from testapp.models import Car, Engine
>>> c = Car.objects.get(name='Audi')
>>> e = Engine.objects.get(name='Diesel')
>>> e.car
ForeignKey with unique=True Example
>>> from testapp.models import Car2, Engine2
>>> c2 = Car2.objects.get(name='Mazda')
>>> e2 = Engine2.objects.get(name='Wankel')
>>> e2.car2_set.all()
[]
Model Code
from django.db import models
class Engine(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Car(models.Model):
name = models.CharField(max_length=25)
engine = models.OneToOneField(Engine)
def __unicode__(self):
return self.name
class Engine2(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Car2(models.Model):
name = models.CharField(max_length=25)
engine = models.ForeignKey(Engine2, unique=True)
def __unicode__(self):
return self.name