📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Insert Warnings
  • NULL and NOT NULL
  • Setting Default Values
  • A Primer on Primary Keys
  1. Other
  2. Mysql Bootcamp

Inserting Data

Here's the basic syntax for inserting data into a table:

INSERT INTO tablename(name, age)
VALUES ('Dan', 27);

Note: You can INSERT multiple data points by just providing more value sets:

INSERT INTO tablename(name, age)
VALUES ('Dan', 27),
      ('John', 28),
      ('Mary', 5);

Insert Warnings

If you insert data in ways that the data type doesn't accept, it will throw a warning that you can view via SHOW WARNINGS;.

Here's some classic warnings:

  • If you surpass your VARCHAR(X) character limit, by default SQL will truncate your string to the limit.

  • If you surpass your INT limit, by default SQL will record the limit as the value.

  • If you provide a string in place of an INT, SQL will default to 0.

NULL and NOT NULL

NULL is a value that means I don't know what this is or there is no specified value.

By default, all columns in a table permit empty values, which become NULL if nothing is passed in during the creation of a row. For example, the following table has 2 columns--name and age--but we only provide a value for name:

INSERT INTO cats(name) VALUES('Alabama');
-- `age` defaults to the value NULL because no value was provided

In order to require that all columns be populated, you need to provide a NOT NULL parameter in your table creation:

CREATE TABLE cats2 (
  name VARCHAR(100) NOT NULL,
  age INT NOT NULL
);

Note: When you don't specify a column value, MySQL falls on a default value. For VARCHAR, that is an empty string. For INT, that is 0. The next section will discuss setting your own default values.

Setting Default Values

To set default values, use this command structure:

CREATE TABLE cats3 (
  name VARCHAR(100) DEFAULT 'no name provided',
  age INT DEFAULT 99
);

Note: You still need to set NOT NULL if you want to disallow NULL values. Without it, NULL values can still be explicitly inserted into your table, which you may not want.

A Primer on Primary Keys

A primary key is a column guaranteed to be unique in a table. This make it possible to distinctly identify an item in a table from every other item--especially when they may have identical values in other columns.

CREATE TABLE unique_cats (
  cat_id INT NOT NULL, -- define column
  PRIMARY KEY (cat_id) -- assign column as primary key!
  
  -- OR

  cat_id INT NOT NULL PRIMARY KEY -- you can also do it on the same line
);

INSERT INTO unique_cats(cat_id, name) VALUES(1, 'Garfield');

Note: Sometimes you don't want to have to explicitly define your primary key. You want MySQL to do it for you. Just add the following flag:

CREATE TABLE unique_cats (
  cat_id INT NOT NULL AUTO_INCREMENT, -- THERE IS YOUR FLAG!
  ...
);
PreviousCreating Databases And TablesNextCRUD Commands

Last updated 3 years ago