Data Definition and Data Types

Slides:



Advertisements
Similar presentations
Creating Databases and E/R Diagrams with SQL Server Management Studio Svetlin Nakov Telerik Corporation
Advertisements

Module 4: Creating Data Types and Tables. Overview Creating Data Types Creating Tables Generating Column Values Generating Scripts.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Svetlin Nakov Technical Trainer Software University
Module 3: Creating Data Types and Tables. Overview Working with Data Types Working with Tables Generating Column Values Generating Scripts.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Data Modeling Creating E/R Diagrams SoftUni Team Technical Trainers Software University
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
SQL Server for Developers Using SQL Server, DB Design, SQL SELECT, INSERT, UPDATE, DELETE SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Version Control Systems
Auto Mapping Objects SoftUni Team Database Applications
Working with Fluent API Inheritance Strategies
Introduction to Entity framework
Fundamentals of DBMS Notes-1.
Databases basics Course Introduction SoftUni Team Databases basics
C# Basic Syntax, Visual Studio, Console Input / Output
Data Structures Course Overview SoftUni Team Data Structures
C# Database Fundamentals with Microsoft SQL Server
Introduction to Entity Framework
Parsing JSON JSON.NET, LINQ-to-JSON
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
C# Databases Advanced with Microsoft SQL Server
Database Design and Rules
EF Relations Object Composition
Entity Framework: Code First
Parsing XML XDocument and LINQ
Repeating Code Multiple Times
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Entity Framework DB From Code, OOP Introduction
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
Basic CRUD in SQL Server
Install and configure theme
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Caching Data in ASP.NET MVC
Fast String Manipulation
Array and List Algorithms
Functional Programming
Processing Variable-Length Sequences of Elements
Transactions in Entity Framework
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Numeral Types and Type Conversion
Combining Data Structures
Best practices and architecture
How to connect natively?
Introduction to Databases
Built-in Functions. Usage of Wildcards
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
Manual Mapping and AutoMapper Library
Functional Programming
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Introduction to TypeScript & Angular
CSS Transitions and Animations
Software Quality Assurance
Version Control Systems
Text Processing and Regex API
CSS Transitions and Animations
Multidimensional Arrays
ORACLE SQL Developer & SQLPLUS Statements
CIS16 Application Programming with Visual Basic
SQL (Structured Query Language)
Presentation transcript:

Data Definition and Data Types Managing DBs using IDEs Data Types Viktor Kostadinov Technical Trainer SQL Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents Data Types Database Modeling Basic SQL Queries and Table Customization Deleting Data and Structures © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Questions sli.do #SQL

Data Types in SQL Server

Data Types in SQL Server Numeric bit (1-bit), integer (32-bit), bigint (64-bit) float, real, numeric(scale, precision) money – for money (precise) operations Strings char(size) – fixed size string varchar(size) – variable size string nvarchar(size) – Unicode variable size string text / ntext – text data block (unlimited size)

Data Types in SQL Server (2) Binary data binary(size) – fixed length sequence of bits varbinary(size) – a sequence of bits, 1-8000 bytes or max (2GB) Date and time date – date in range 0001-01-01 through 9999-12-31 datetime – date and time with precision of 1/300 sec smalldatetime – date and time (1-minute precision)

Data Definition using Management Studio Database Modeling Data Definition using Management Studio

Working with Object Explorer Object Explorer is the main tool to use when working with the database and its objects Enables us: To create a new database To create objects in the database (tables, stored procedures, relationships and others) To change the properties of objects To enter records into the tables

Creating a New Database Select New Database from the context menu under "Databases" You may need to Refresh [F5] to see the results

Creating Tables From the context menu under "Tables" inside the desired database Table name can be set from its Properties [F4] or when it is saved

Creating Tables (2) A Primary Key is used to uniquely identify and index records Click Set Primary Key from the context menu of the desired row

Creating Tables (3) Identity means that the values in a certain column are auto incremented for every newly inserted record These values cannot be assigned manually Identity Seed – the starting number from which the values in the column begin to increase. Identity Increment – by how much each consecutive value is increased

Creating Tables (4) Setting an identity through the "Column Properties" window

Storing and Retrieving Data We can add, modify and read records with Management Studio To insert or edit a record, click Edit from the context menu Enter data at the end to add a new row

Storing and Retrieving Data (2) To retrieve records, click Select from the context menu The received information can be customized with SQL queries

Changes cannot conflict with existing rules! Altering Tables You can change the properties of a table after it's creation Select Design from the table's context menu Changes cannot conflict with existing rules!

Data Definition using T-SQL Basic SQL Queries Data Definition using T-SQL

SQL Queries We can communicate with the database engine using SQL Queries provide greater control and flexibility To create a database using SQL: SQL keywords are traditionally capitalized Database name CREATE DATABASE Employees

Table Creation in SQL Table name CREATE TABLE People ( Id int NOT NULL, Email varchar(50) NOT NULL, FirstName varchar(50), LastName varchar(50) ) Custom properties Column name Data type

Retrieve Records in SQL To get all information from a table You can limit the columns and number of records Table name SELECT * FROM Employees List of columns Number of records SELECT TOP (5) FirstName, LastName FROM Employees

Adding Rules, Constraints and Relationship Table Customization Adding Rules, Constraints and Relationship

Custom Column Properties Primary Key Identity (auto-increment) Unique constraint – no repeating values in entire table Id int NOT NULL PRIMARY KEY Id int IDENTITY PRIMARY KEY Email varchar(50) UNIQUE

Custom Column Properties (2) Default value – if not specified (otherwise set to NULL) Value constraint Balance decimal(10,2) DEFAULT 0 Kelvin float(10,2) CHECK (Kelvin > 0)

Changing Table Properties After Creation Altering Tables Changing Table Properties After Creation

Altering Tables Using SQL A table can be changed using the keywords ALTER TABLE Add new column ALTER TABLE Employees Table name ALTER TABLE Employees ADD Salary money Column name Data type

Altering Tables Using SQL (2) Delete existing column Modify data type of existing column Column name ALTER TABLE People DROP COLUMN FullName ALTER TABLE People ALTER COLUMN Email varchar(100) Column name New data type

Altering Tables Using SQL (3) Add primary key to existing column Add unique constraint Constraint name ALTER TABLE People ADD CONSTRAINT PK_Id PRIMARY KEY (Id) Column name (more than one for composite key) Constraint name ALTER TABLE People ADD CONSTRAINT uq_Email UNIQUE (Email) Columns name(s)

Altering Tables Using SQL (4) Set default value Add check constraint ALTER TABLE People ADD DEFAULT 0 FOR Balance Default value Column name Constraint name ALTER TABLE InstrumentReadings ADD CONSTRAINT PositiveValue CHECK (Kelvin > 0) Condition

Deleting Data and Structures

Deleting from Database Deleting structures is called dropping You can drop keys, constraints, tables and entire databases Deleting all data in a table is called truncating Both of these actions cannot be undone – use with caution!

Dropping and Truncating To delete all the entries in a table To drop a table – delete data and structure To drop entire database TRUNCATE TABLE Employees Table name DROP TABLE Employees Table name Database name DROP DATABASE AMS

Dropping and Truncating (2) To remove a constraining rule from a column This includes primary keys, value constraints and unique fields To remove default value (if not specified, revert to NULL) Table name ALTER TABLE Employess DROP CONSTRAINT pk_Id Constraint name Table name ALTER TABLE Employess ALTER COLUMN Clients DROP DEFAULT Columns name

Summary Table columns have a fixed type Setting up the database is the last step of the design process We can use Management Studio to create and customize tables SQL provides greater control CREATE TABLE People ( Id int NOT NULL, Email varchar(50) NOT NULL, FirstName varchar(50), LastName varchar(50) ) © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Data Definition and Data Types https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license "C# Part I" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.