1 00:00:00,250 --> 00:00:04,740 Table columns are defined in the schema to have certain data types. 2 00:00:04,740 --> 00:00:08,280 A data type describes the value to be stored. 3 00:00:08,280 --> 00:00:11,970 There are several data types you'll come across on a regular basis. 4 00:00:11,970 --> 00:00:15,950 There are text types, numeric types and date types. 5 00:00:15,950 --> 00:00:19,570 Text types are good for storing names or descriptions of things. 6 00:00:20,750 --> 00:00:25,380 Numeric types are good for storing prices, ages, and quantities of things. 7 00:00:26,540 --> 00:00:28,780 Dates are good for anything time related. 8 00:00:30,320 --> 00:00:34,970 While this isn't a comprehensive list of data types, text, numeric, and 9 00:00:34,970 --> 00:00:37,860 date types are a good starting point. 10 00:00:37,860 --> 00:00:42,700 The reason we use types will become more important as we write SQL later on. 11 00:00:42,700 --> 00:00:46,070 To help you envisage how a schema enforces data types in 12 00:00:46,070 --> 00:00:50,700 columns, you can think of the schema as a coin sorter. 13 00:00:50,700 --> 00:00:54,940 A coin sorter is used to sort different types of coins into columns. 14 00:00:54,940 --> 00:00:58,110 The coin sorter ensures that only the correct coin types go 15 00:00:58,110 --> 00:00:59,880 into the correct column. 16 00:00:59,880 --> 00:01:00,810 Like the coin sorter, 17 00:01:00,810 --> 00:01:06,150 the database schema prescribes what data should go into each column of the table. 18 00:01:06,150 --> 00:01:08,820 You may be asking, why even do this? 19 00:01:08,820 --> 00:01:13,720 Let's look at some examples to see why it's important to have data types. 20 00:01:13,720 --> 00:01:17,160 Imagine everything in this product table was just text. 21 00:01:17,160 --> 00:01:21,070 The name, price, stock count, are all text. 22 00:01:21,070 --> 00:01:26,090 If I wanted to sort by the current stock count, what result do you think I'd get? 23 00:01:26,090 --> 00:01:28,050 Well, it'd be sorted like this. 24 00:01:29,070 --> 00:01:34,200 This is because the stock count is sorted alphabetically, and not numerically. 25 00:01:34,200 --> 00:01:38,530 Let's change the price and the stock count to a numeric type and 26 00:01:38,530 --> 00:01:40,956 sort by stock count again. 27 00:01:40,956 --> 00:01:42,179 That looks better. 28 00:01:42,179 --> 00:01:46,877 Having correct data types in your database not only helps with sorting but 29 00:01:46,877 --> 00:01:48,467 it also comes in handy for 30 00:01:48,467 --> 00:01:53,179 many other applications such as generating the sum of all sales figures. 31 00:01:53,179 --> 00:01:54,583 Generating a sum and 32 00:01:54,583 --> 00:02:00,730 other mathematical operations can only be done by using data with a numeric type. 33 00:02:00,730 --> 00:02:04,170 Trying to do math with text is impossible. 34 00:02:04,170 --> 00:02:08,830 In the case of an events table for an event planner where the new data is added 35 00:02:08,830 --> 00:02:14,050 all the time, the data is retrieved in the order it was entered by default. 36 00:02:14,050 --> 00:02:16,880 If the date of the event column was just text and 37 00:02:16,880 --> 00:02:19,280 you attempted to sort it, it would look like this. 38 00:02:20,600 --> 00:02:24,240 However if the date of the event was correctly a date type, 39 00:02:24,240 --> 00:02:25,460 it would sort like this. 40 00:02:27,890 --> 00:02:32,240 Understanding that column data types can affect how data is retrieved 41 00:02:32,240 --> 00:02:35,590 helps people design the schema for the database. 42 00:02:35,590 --> 00:02:38,850 While we're not designing the schema ourselves in this course, 43 00:02:38,850 --> 00:02:42,320 understanding data types will also help us understand 44 00:02:42,320 --> 00:02:45,360 how we compose the SQL queries throughout this course.