-
Notifications
You must be signed in to change notification settings - Fork 0
/
EO1.sql
116 lines (85 loc) · 2.5 KB
/
EO1.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Multi Line Comment
SQL Server Basics
SQL Syntax
- UPPERCASE - T-SQL Keyword
- SELECT, UPDATE, DELETE, WHERE
- Uppercase is not required
- italics = user defined parameter
- bold = Database object
- [] = Allows us to use spaces and special characters ([Movie Title])
- ; to end line
Fully Qualified Name
[server].[database].[schema].[table]
*/
-- Single Line Comment
--SELECT * FROM MSreplication_options;
--Batch Operations
-- End of the batch: GO
-- DDL - Create Tables
-- Employees and Departments - What would happen if Employees table got created first? Wouldn't Work!
/*
CREATE Departments;
GO
CREATE Employees;
GO
*/
-- sqlcmd -S localhost to run from command line
--This does not define different transactions, this is for scripting purposes.
/*
Other ways to interact with server
CLI -sqlcmd
Programmatically with a Programming Language
JDBC
We can use Python with the pymssql library
*/
-- DML/CRUD Application
-- DML = Changing, updating data
/*
Database Operations
- DDL
-CREATE/ALTER/DROP tables/views/sequences
- DML
-INSERT/SELECT/DELETE/UPDATE
- TCL
-COMMIT/ROLLBACK/ BEGIN TRAN
-Default: Auto-Commit
*/
SELECT * FROM INFORMATION_SCHEMA.COLUMNS;
/*
CRUD
CREATE -- INSERT INTO
READ - SELECT
UPDATE - UPDATE
DELETE - DELETE
*/
CREATE DATABASE demo;
use demo;
--CREATE TABLE creates a table
-- By default it will be put in dbo schema
-- We can put it into a different Schema, but we will have to create the Schema first
CREATE TABLE users (
user_id int PRIMARY KEY IDENTITY, -- Identity is a surrogate key. Starts at 1 and counts up
user_first_name VARCHAR(30) NOT NULL,
user_last_name VARCHAR(30) NOT NULL,
user_email_id VARCHAR(50) NOT NULL,
user_email_validated bit DEFAULT 0,
user_password VARCHAR(200),
user_role VARCHAR(30) NOT NULL DEFAULT 'U',
is_active bit DEFAULT 0,
last_updated_ts DATETIME DEFAULT getdate()
);
-- Typically our applications will not DDL, we willc onnect to already established dbs
--CRUD
-- (C)REATE INSERT
-- Syntax: INSERT INTO <table> (col1, col2, col3) VALUES ( val1, val2, val3)
INSERT INTO users ( user_first_name, user_last_name, user_email_id)
VALUES ('Scott', 'Tiger', '[email protected]');
SELECT * FROM users;
INSERT INTO users
(user_first_name, user_last_name, user_email_id, user_password, user_role, is_active)
VALUES
('Sora', 'Hearts', '[email protected]', '019he221', 'U', 1),
('Minnie', 'Mouse', '[email protected]', 'fhuih1234', 'U', 1),
('Max', 'Goof', '[email protected]', 'j4892hyf1', 'U', 1);
SELECT * FROM users;