Sitemap
Arfin Parween

Arfin Parween

Software Engineer | Content Creator

๐‘บ๐’•๐’†๐’‘-๐’ƒ๐’š-๐‘บ๐’•๐’†๐’‘ ๐‘ฎ๐’–๐’Š๐’…๐’†: ๐‘ฏ๐’๐’˜ ๐’•๐’ ๐‘ช๐’“๐’†๐’‚๐’•๐’† ๐’‚ ๐‘ซ๐’†๐’๐’•๐’‚ ๐‘ป๐’‚๐’ƒ๐’๐’† ๐’Š๐’ ๐‘ซ๐’‚๐’•๐’‚๐’ƒ๐’“๐’Š๐’„๐’Œ๐’”?

2 min readNov 6, 2024

--

Delta table can be created in multiple ways depending on your needs. In this post, Iโ€™ll walk you through three different methods to create a Delta table in Databricks. Whether youโ€™re working with PySpark, SQL commands, or using the Delta API, each method offers unique advantages for different use cases.

Method 1: Using Delta API (Python)

from delta.tables import *

DeltaTable.createIfNotExists(spark)\
.tableName(โ€œStudentโ€)\
.addColumn(โ€œIDโ€, โ€œINTโ€)\
.addColumn(โ€œNameโ€, โ€œSTRINGโ€)\
.addColumn(โ€œDepartmentโ€, โ€œSTRINGโ€)\
.addColumn(โ€œAgeโ€, โ€œINTโ€)\
.property(โ€œDescriptionโ€, โ€œStudent tableโ€)\
.location(โ€˜/FileStore/table/delta/createtableโ€™)\
.execute()

Method 2: Using SQL Syntax

%sql
CREATE TABLE IF NOT EXISTS Employee_02 (
ID INT,
Name STRING,
Department STRING,
Age INT
) USING DELTA

Method 3: Using DataFrame and saveAsTable (PySpark)

data = [(1, โ€˜Arfinโ€™, โ€˜CSEโ€™), (2, โ€˜Pujaโ€™, โ€˜ECEโ€™), (3, โ€˜Marryโ€™, โ€˜CEโ€™)]
columns = [โ€œIDโ€, โ€œNameโ€, โ€œDepartmentโ€]

Get Arfin Parweenโ€™s stories inย yourย inbox

Join Medium for free to get updates fromย thisย writer.

df = spark.createDataFrame(data, columns)
df.show()

df.write.format(โ€œdeltaโ€).saveAsTable(โ€œEmployee_03โ€)

By following these three methods, you can easily create and manage Delta tables in Databricks for your data engineering workflows. Each method has its own use case โ€” whether youโ€™re creating a table programmatically, using SQL commands, or working with DataFrames.

Connect with me: https://topmate.io/arfinparween

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

--

--

Arfin Parween
Arfin Parween

Written by Arfin Parween

Software Engineer | Content Creator