Loading
Lesson 25
Courses / Software School Cuts
Answering Questions About SQL (Id Primary Key, Varchar, Sorting)

Students ask some questions related to Structured Query Language (SQL) and get a response.

Is id always the primary key without specifying it?

What is VARCHAR(100)?

How to sort data in a specific order, ascending or descending?

Summary

Summary of Transcript

Q&A Session

  • Primary Key Specification:
    • It's necessary to explicitly specify a column as a primary key when creating a table in SQL. Although in some environments, tables may be created automatically with defaults, in practice, you write a CREATE TABLE statement where you define each column, including which ones are primary keys.
    • When using frameworks like ORM (Object Relational Mapping), the configuration is often handled behind the scenes, simplifying the process for developers.

Data Types in SQL

  • Understanding BearCare 100:
    • Every column in SQL has a specific data type, such as integers for numbers or varying characters for text.
    • The term "BearCare 100" refers to a character varying string (varchar) that allows for a variable length of characters, bounded by a specified limit—in this case, 100 characters.

Sorting Data

  • Sorting Data in SQL:
    • To sort data in SQL, you use the ORDER BY clause.
    • The default is ascending order, but you can specify descending order using DESC. For instance, to sort customer IDs from highest to lowest, you would execute SELECT * FROM customers ORDER BY customer_id DESC.

Video Transcript

Anybody have any questions? Is ID always the primary key without specifying it? No, you have to specify that a column is a primary key when you make the SQL statement to create the table. Because we are in this environment, the tables were created for us automatically, but in the real world, when you have to make your own table, you're going to write a statement called create table, and then you're going to specify each column, and there you're going to say, is this column primary key or not? So you have to do it. But if you're using any kind of framework for programming like a web server, what we call ORM, Object Relational Mapper, they conveniently have all these things like built in for you, so you probably wouldn't have to do it manually, but in practice, it's behind the scenes, it's there, somebody had to add it. What is BearCare 100? So every column has a type in SQL. Like you can store numbers, right? You can store text. You can even store binary files, like I don't know, images or whatever. So if you look at the left-hand side here, customer ID is an int, int is like integer, which stands for just a number, that's an integer, right? One, two, three, and so on. And then first name is BearCare 100. BearCare is a character varying string, meaning it's a string of characters that vary in length. So I can have one character, I can have two, I can have three and so on, but it has to be a limit, right? How many characters can you have? That's what you put in the, usually what the 100 there is for. It's just by kind of a limit. Okay, I want this varying character, but how far can you go? Can I have more than three characters? And so on. Yeah, look up the docs of BearCare and it will tell you there what number. If you have more information, some of them are that different kind of things, depending on the type. All right, so... How to sort data in a specific order, ascending or descending? We didn't talk about that, but there is a way. Let me see if... I can show you right now, even though I wasn't planning on talking about that. So let's say, you can see now the columns start from customer ID one to seven, right? What if I want to reverse that? So I can say select start from customers and this will give me the table as is. Now if I want to order by some column, I add order by here. Literally the word order space by. And then customer ID is the column I want to order by. But then I got to say, is it ascending or descending? By default, it's ascending, but if you want to send it, D E S C, which stands for descending. So as you can see, now I have the rows descending by customer ID column. Okay, so that's the order by.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: