Ένας τύπος δεδομένων ορίζει το είδος της τιμής που μπορεί να περιέχει ένα πεδίο. Ακέραιος, δεκαδικός, συμβολοσειρά, ημερομηνία, λογικός, δυαδικός. Ο τύπος καθορίζει ποιες πράξεις είναι έγκυρες και πόσο χώρο καταλαμβάνει η τιμή. Μπορείτε να προσθέσετε δύο ακέραιους αριθμούς. Δεν μπορείτε να προσθέσετε έναν ακέραιο και μια ημερομηνία χωρίς μετατροπή. Μπορείτε να ταξινομήσετε τις συμβολοσειρές αλφαβητικά. Δεν μπορείτε να τις ταξινομήσετε αριθμητικά εκτός εάν περιέχουν αριθμούς. Ο τύπος είναι μια σύμβαση μεταξύ των δεδομένων και του συστήματος που τα αποθηκεύει.
Choosing the wrong type causes problems. Storing a phone number as an integer drops leading zeros. Storing a price as a floating-point number introduces rounding errors that accumulate in financial calculations. Storing a date as a string makes range queries slow and error-prone. Storing a boolean as a string wastes space and invites typos like "ture". Each type has a purpose. Integers for counts. Decimals for money. Strings for text. Dates for temporal values. Booleans for true or false. Binary for files and hashes. Some databases add specialized types: JSON for semi-structured data, arrays for lists, geospatial types for coordinates. The type system enforces consistency. It catches errors at insert time rather than at query time. That early detection is worth the constraints it imposes.
Common data types
- Integer — whole numbers, counts, IDs
- Decimal — precise fractional numbers, money
- String — text of variable length
- Date and time — temporal values
- Boolean — true or false
- Binary — raw bytes, files, hashes
Data types are guardrails. They keep bad values out and make good values easier to work with.
Comments
No comments yet. Be the first to share a thought.
Leave a comment