友谊关系 - sql

时间:2017-06-15 07:26:03

标签: mysql sql

我搜索过该网站并找到了一些示例,但并不是我的问题所特有的。

我正在用SQL创建一个frienship表,我想确保每一行都是唯一的。例如:如果用户x 用户y --->的朋友用户y 是x的朋友。 我试图让它无法进入(创建表格时):

user1ID  user2ID        timeStamp
11        10        01/01/2010 00:00:00
10        11        01/01/2010 00:00:00

我写了这个:

CREATE TABLE FriendRelationship(
    User1ID INTEGER NOT NULL,
    User2ID INTEGER NOT NULL,
    **CHECK (User1ID <> User2ID), /*User x cannot be a frien with himself*/
    CHECK ((User1ID <> all User2ID) AND (User2ID <> all( User1ID))),     
    /*if user x friend with user y ->  user y is friend with user x */**
    FtimeStamp DATETIME NOT NULL,
    FOREIGN KEY (User1ID) REFERENCES UserDF(UserID),
    FOREIGN KEY (User2ID) REFERENCES UserDF(UserID),
    PRIMARY KEY (User1ID, User2ID));

1 个答案:

答案 0 :(得分:0)

我会采取不同的方法。由于哪个用户<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Demo</title> <style> #map { height: 600px; border: 1px solid #ccc; } .myCss{ text-align:center; } </style> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" /> <link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.0.3/dist/MarkerCluster.css" /> <link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.0.3/dist/MarkerCluster.Default.css" /> <link href='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/leaflet.fullscreen.css' rel='stylesheet' /> </head> <body> <div id="map"></div> <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script> <script src="https://unpkg.com/leaflet.markercluster@1.0.3/dist/leaflet.markercluster-src.js"></script> <script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/Leaflet.fullscreen.min.js'></script> <script> var addressPoints = [ [-31.953512, 115.857048, "<p class='myCss'><b>Morgan Stanley<br> Funder</b><p>"], [-37.8210819833, 175.2213903167, "<p class='myCss'><b>Morgan Stanley<br> Funder</b><p>"], [-34.206841, 142.136490, "<p class='myCss'><b>Morgan Stanley<br> Funder</b><p>"], [-37.8211946833, 175.2213655333, "<p class='myCss'><b>Morgan Stanley<br> Funder</b><p>"] ]; </script> <script type="text/javascript"> var tiles = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {}); latlng = L.latLng(-25.2744, 133.7751); var map = L.map('map', {center: latlng, zoom: 4, layers: [tiles], fullscreenControl: { pseudoFullscreen: false } }); var markers = L.markerClusterGroup(); for (var i = 0; i < addressPoints.length; i++) { var a = addressPoints[i]; var title = a[2]; var marker = L.marker(new L.LatLng(a[0], a[1]), { title: title }); marker.bindPopup(title); markers.addLayer(marker); } map.addLayer(markers); </script> </body> </html>以及user1id无关紧要,我只能确定user2id必须始终是较小的数字(并且应用程序负责对这两个用户进行排序)插入时ID正确)。完成后,两者组合的主键将确保您没有重复的条目:

user1id