SQL Mastery for Interviews
Back to course sections
  • SQL Mastery for Interviews
    • How to Learn SQL Step by Step SQL: Select, Where, From Filtering: LIKE, IN, BETWEEN, AND, OR, NOT Joins in SQL Grouping in SQL SQL Aggregate Functions SQL Set Operators What Are Constraints in SQL? SQL Window Functions Transactions in SQL Common Table Expressions (CTE) in SQL SQL Interview Questions Normalization in SQL Best SQL Interview Practice Questions Useful SQL Commands with Examples Best SQL Projects for Beginners What Is DDL? Data Definition Language Explained SQL Window Functions Interview Questions
    Email Facebook LinkedIn Reddit Twitter
    Mark As Completed Discussion

    What is Data Definition Language (DDL)?

    Simply put, DDL is a subset of SQL commands that we use to create and modify the structure of objects in a database. We can change the database structure by adding new tables or objects with all of their attributes such as data type, table name, and so on.

    To gain a better understanding, let's delve deeper into each DDL command shown in the diagram. To demonstrate these commands, let's pretend we're working on a database for a fictitious music store Sequel Records.

    Introduction

    CREATE

    The DDL command CREATE is used to create the database or any objects such as a table, index, function, view, and store procedures.

    1. CREATE DATABASE

    Firstly we must create the database for our store. The CREATE query below allows you to create a new database.

    2. CREATE TABLE

    We firstly want to create a table containing all of the information on the albums we sell. Within the CREATE statement, we must first define the structure of our table, including column names, data types, and the size of the data to be added to each column.

    CREATE

    From there we can use Data Manipulation Language (DML) specifically the command INSERT to populate the table with the data from our albums.

    3. CREATE VIEW

    Suppose we run a query and want to save the results-set as a table, in this situation we can create a view using the CREATE command. Because a view displays current data, the database engine recreates it each time a user queries it.

    We can then run queries on our view as if it was a separate table.

    CREATE

    4. CREATE INDEX

    Using the CREATE command we can create indexes for our table. Indexes are used to retrieve data from a database more quickly than would otherwise be possible. The indexes are not visible to users; they are only used to speed up searches and queries.

    As an example, our database becomes increasingly populated with albums over time, to the point where searching becomes extremely slow. Using the following command, we create an index on a single column title or more than one column title and artist.

    In addition, we can make indexes unique in order to maintain data integrity by ensuring that no two rows of data in a table have the same key value.

    5. CREATE PROCEDURE

    The CREATE command can also be used to create a stored procedure which is basically a prepared SQL code that can be saved and reused over and over again.

    To execute our stored procedure we simply run the command below. Handy right?

    CREATE

    Try this exercise. Click the correct answer from the options.

    You want to create a table called artists. Fill in the blank

    ALTER

    1. ALTER TABLE

    The DDL command ALTER is used to change the structure of the databases we create. For example, you might want to be able to keep track of inventory with your new database. You decide to add a column called qty_on hand.

    Or you notice that the data type you entered for album_id is an integer, when you actually wanted it to be a VARCHAR of length 3.

    Try this exercise. Click the correct answer from the options.

    You want to add a column called nationality to the table artists. Fill in the blank

    DROP

    Using the DROP command we can delete the database or any objects associated with it.

    1. DROP DATABASE

    Using the following command we can delete our entire database. Before running this command, you should be aware that it will delete your entire database including all tables, data, indexes, and other elements, so make sure you're certain before proceeding.

    2. DROP TABLE

    Similar to dropping a database, when we drop a table everything will be deleted.

    3. DROP VIEW

    And to drop a view.

    3. DROP COLUMN

    Let's say we want to delete a specific column. In this case we use the ALTER TABLE command in conjunction with the DROP command. As the owner of the music store, you determine that the column year_published is no longer necessary in this table and decide to remove it.

    4. DROP CONSTRAINT

    Another useful feature of this command is the ability to remove constraints. Below we will go through the syntax for removing all constraints with the command DROP.

    5. DROP INDEX

    Imagine we no longer have as much albums in our table therefore there is no real need for the index we created in the first part of this tutorial. We can remove the index using the DROP command.

    Try this exercise. Click the correct answer from the options.

    You want to delete the table artists you just made. Fill in the blank

    TRUNCATE

    The TRUNCATE TABLE command deletes the data within a table. Unlike DROP, it keeps the table's structure, indexes, and dependencies. If we wanted to remove all data entries from a table in a database while keeping the table albums structure but not the entries, we would use the SQL command below.

    TRUNCATE

    Build your intuition. Is this statement true or false?

    Truncating a table deletes the tables structure.

    RENAME

    The DDL command RENAME, as expected is used to change names in our database.

    1. RENAME TABLE

    Perhaps we should choose a more appropriate name for our table in our music store. We can do so with the following.

    2. RENAME COLUMN

    Or maybe we want to rename a column, we can do the following.

    Let's test your knowledge. Click the correct answer from the options.

    You want to rename the column year_published to release_year. Fill in the blank

    One Pager Cheat Sheet

    Programming Categories

  • Basic Arrays Interview Questions
  • Binary Search Trees Interview Questions
  • Dynamic Programming Interview Questions
  • Easy Strings Interview Questions
  • Frontend Interview Questions
  • Graphs Interview Questions
  • Hard Arrays Interview Questions
  • Hard Strings Interview Questions
  • Hash Maps Interview Questions
  • Linked Lists Interview Questions
  • Medium Arrays Interview Questions
  • Queues Interview Questions
  • Recursion Interview Questions
  • Sorting Interview Questions
  • Stacks Interview Questions
  • Systems Design Interview Questions
  • Trees Interview Questions
  • Popular Lessons

  • All Courses, Lessons, and Challenges
  • Data Structures Cheat Sheet
  • Free Coding Videos
  • Bit Manipulation Interview Questions
  • Javascript Interview Questions
  • Python Interview Questions
  • Java Interview Questions
  • SQL Interview Questions
  • QA and Testing Interview Questions
  • Data Engineering Interview Questions
  • Data Science Interview Questions
  • Blockchain Interview Questions
  • Exception Handling
  • Javascript Build Tools and Bundlers
  • What are Strings and String Operations?
  • Concurrency and Multithreading
  • Pandas Architecture
  • 深圳SEO优化公司滨州SEO按天计费公司厦门优化报价甘南网站搜索优化报价郴州百搜标王沈阳推广网站推荐迁安网站建设价格塔城外贸网站建设推荐大理seo优化哪家好坑梓百姓网标王报价常州建网站公司郑州模板网站建设坪地优化哪家好伊春营销网站报价迪庆网站优化排名长沙网站改版宝鸡品牌网站设计价格汕头seo网站优化哪家好永湖百姓网标王多少钱马鞍山百姓网标王价格南澳外贸网站建设公司广元营销型网站建设报价郴州百度标王多少钱重庆关键词排名价格南通seo网站优化价格丽江关键词排名包年推广报价玉林百度标王哪家好酒泉seo网站推广价格衡水英文网站建设福永网站关键词优化多少钱荷坳英文网站建设报价歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

    深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化