We create databases and tables to organize and store data in a structured way. This helps us find and use data easily later.
Creating databases and tables in Hadoop
CREATE DATABASE database_name; CREATE TABLE table_name ( column1_name column1_type, column2_name column2_type, ... ) STORED AS file_format;
The CREATE DATABASE command makes a new database.
The CREATE TABLE command makes a new table inside a database.
CREATE DATABASE sales_data;
CREATE TABLE sales_data.customers ( id INT, name STRING, age INT ) STORED AS PARQUET;
CREATE TABLE employees ( emp_id INT, emp_name STRING, salary FLOAT ) STORED AS ORC;
This code creates a database called company. Then it creates a table employees inside it with four columns. Finally, it lists all databases and tables in the company database.
CREATE DATABASE company; CREATE TABLE company.employees ( emp_id INT, emp_name STRING, department STRING, salary FLOAT ) STORED AS PARQUET; SHOW DATABASES; SHOW TABLES IN company;
Database and table names should be unique and meaningful.
Choose the file format (like PARQUET or ORC) based on your data size and query needs.
Use SHOW DATABASES; and SHOW TABLES IN database_name; to check what you created.
Databases help organize data into separate groups.
Tables store data in rows and columns inside databases.
Creating databases and tables is the first step to working with data in Hadoop.
Practice
Solution
Step 1: Understand the role of a database
A database is used to organize data logically into separate groups or collections.Step 2: Differentiate database from tables
Tables store data in rows and columns inside a database, but the database itself is for grouping.Final Answer:
To organize data into separate groups -> Option CQuick Check:
Database = Organize data groups [OK]
- Confusing database with table
- Thinking database stores data directly
- Assuming database speeds up queries automatically
sales_db in Hive?Solution
Step 1: Recall Hive syntax for database creation
The correct command to create a database in Hive isCREATE DATABASE database_name;.Step 2: Check each option
Only CREATE DATABASE sales_db; matches the correct syntax exactly.Final Answer:
CREATE DATABASE sales_db; -> Option AQuick Check:
CREATE DATABASE = correct syntax [OK]
- Using MAKE or NEW instead of CREATE
- Abbreviating DATABASE as DB
- Missing semicolon at end
CREATE TABLE employees (id INT, name STRING, salary FLOAT);
Solution
Step 1: Analyze the CREATE TABLE statement
The query uses correct syntax to create a table named employees with columns id, name, and salary.Step 2: Check for errors or missing parts
Database name is optional if default database is used; no syntax error here.Final Answer:
Creates a table named employees with three columns -> Option AQuick Check:
CREATE TABLE with columns = creates table [OK]
- Thinking database name is mandatory
- Confusing table and database creation
- Assuming syntax error without checking carefully
CREATE TABLE users id INT, name STRING;
Solution
Step 1: Review the syntax for CREATE TABLE
Column definitions must be enclosed in parentheses after the table name.Step 2: Check the given query
The query misses parentheses aroundid INT, name STRING, causing syntax error.Final Answer:
Missing parentheses around column definitions -> Option BQuick Check:
Columns need parentheses [OK]
- Forgetting parentheses
- Assuming table name is wrong
- Thinking data types are invalid
company_db and inside it a table departments with columns dept_id INT and dept_name STRING. Which sequence of commands is correct?Solution
Step 1: Create the database first
UseCREATE DATABASE company_db;to make the database.Step 2: Switch to the new database
UseUSE company_db;to set the context for table creation.Step 3: Create the table with correct syntax
UseCREATE TABLE departments (dept_id INT, dept_name STRING);inside the database.Final Answer:
CREATE DATABASE company_db; USE company_db; CREATE TABLE departments (dept_id INT, dept_name STRING); -> Option DQuick Check:
Create DB, switch DB, then create table [OK]
- Trying to create table before database
- Wrong order of USE and CREATE DATABASE
- Missing parentheses in CREATE TABLE
Ready to prove it?
Pass the test and this pattern moves unknown
