SQL – Column constraints

Slides:



Advertisements
Similar presentations
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Jennifer Widom Relational Databases The Relational Model.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Joining Tables Steve Perry
Getting started with Accurately Storing Data
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Fundamentals of DBMS Notes-1.
Rob Gleasure robgleasure.com
SQL – join.
Logical DB Design: ER to Relational
Rob Gleasure robgleasure.com
CS320 Web and Internet Programming SQL and MySQL
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Multiple Table Queries
 2012 Pearson Education, Inc. All rights reserved.
Basic select statement
SQL – More Table Constraints
Relational Databases.
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
LESSON Database Administration Fundamentals Inserting Data.
Database Management  .
Translation of ER-diagram into Relational Schema
Writing Basic SQL SELECT Statements
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL – Python and Databases (Continued)
Index Structure.
Chapter 4 The Relational Model Pearson Education © 2009.
The Relational Model Relational Data Model
Chapter 4 The Relational Model Pearson Education © 2009.
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Restricting and Sorting Data
PHP and MySQL.
Chapter 4 Indexes.
What is a Database? A collection of data organized in a manner that allows access, retrieval, and use of that data.
CH 4 Indexes.
Oracle Join Syntax.
Introduction To Structured Query Language (SQL)
The Relational Model Transparencies
CMPT 354: Database System I
CH 4 Indexes.
Writing Basic SQL SELECT Statements
CMPT 354: Database System I
Getting to First Base: Introduction to Database Concepts
Introduction To Structured Query Language (SQL)
CS3220 Web and Internet Programming SQL and MySQL
Getting to First Base: Introduction to Database Concepts
A Very Brief Introduction to Relational Databases
IST 318 Database Administration
CS3220 Web and Internet Programming SQL and MySQL
Displaying Data from Multiple Tables
កម្មវិធីបង្រៀន SQL Programming ជាភាសាខ្មែរ Online SQL Training Course
SQL NOT NULL Constraint
SQL AUTO INCREMENT Field
Presentation transcript:

SQL – Column constraints

Unique Used to specify a column whose values will always be unique for every row. CREATE TABLE students (msu_id TEXT UNIQUE, hackerrank TEXT UNIQUE, first_name TEXT); -- Not Unique The DBMS will raise an error if the constraint is violated.

Should null(s) be allowed in a unique column? No Only One As many as desired Depends on the data type of column

NOT NULL Used to designate columns that may not be NULL CREATE TABLE students (msu_id TEXT UNIQUE NOT NULL, section INTEGER NOT NULL); Raises error if a NULL value is inserted.

Primary Key Each table can have at most one PRIMARY KEY. It is implicitly NOT NULL and UNIQUE It is used to specify a specific row in the table CREATE TABLE students (msu_id TEXT PRIMARY KEY, hackerrank TEXT UNIQUE NOT NULL, grade REAL);

Composite Primary KEY You can create a primary key out of multiple columns if the combination is unique CREATE TABLE students (first_name TEXT, last_name TEXT, PRIMARY KEY (first_name, last_name)); first_name last_name Tianhang Sun Angela Joseph Norwood

Why use primary Keys? They indicate values that can be used to easily connect two relations together (joins). They indicate to the database good attributes to organize the rows by. This allows for more optimized queries. Best practice is for every table to have a primary key

Which column(s) would make the best Primary key for a table? Social Security Number First and Last Name Student Number MSU Net ID

Using qualified Table NAmes The qualified name of a table is <name of database>.<name of relation> (e.g. main.students) The qualified name of a column is <qualified relation>.<name of column> (e.g. main.students.msu_id) Sometimes it is simpler to preface column names with their relation (we'll see why shortly) SELECT name FROM students; Is exactly the same as: SELECT students.name FROM students;

Qualified Column Names You can also use the pattern anywhere you use a column name: SELECT * FROM students WHERE students.name = 'Josh';

SQL Alias (as) Another helpful piece of syntax is aliases if you want to refer to a column by a different name. SELECT msu_net_id AS id FROM students WHERE id = 'nahumjos'; You can also use it on tables: SELECT cse480.msu_net_id FROM students AS cse480; SELECT cse480.msu_net_id FROM students cse480;