How to design SQL tables for these requirements

I have data like CITYand AREA.

  • Everyone CITYhas severalAREAS
  • Each AREAhas several AREAS(there is no end, dynamically the user can add AREASunder the child AREA, like AREA->AREA->Area->AREA....->AREA)

So, how to create a table structure that satisfies these requirements?

Thanks in advance.

+3
source share
5 answers

City table

  • CityID (PK)
  • Cityname

City square table

  • CityID (composite (two columns) PKey)
  • AreaID (Add a unique index to this column if you want the area to be accessible only to one city)

Area table

  • AreaID (PK)
  • Areaname

Area Area

  • AreaID ( ) ( ( ) PKey)
  • AreaID

  • , Areas.
  • Area Area , . , . AreaID - , AreaID.
+2

:

  • id (PK)
  • parent_area_id - , , NULL, (FK AREA)
  • city_id - , , - -, city_id NULL, parent_area_id , (FK ).

CITY:

  • id (PK)

MySQL -

+1

(, , ). , , . "" . Joe Celko SQL.

+1

.

enter image description here

, .

enter image description here

+1

SQL Server 2008 -

+1
source

All Articles