-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy path10.Revist Data Types.sql
173 lines (124 loc) · 3.95 KB
/
10.Revist Data Types.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* data prep */
CREATE TABLE people (name VARCHAR(100), birthdate DATE, birthtime TIME, birthdt DATETIME);
INSERT INTO people (name, birthdate, birthtime, birthdt)
VALUES('Padma', '1983-11-11', '10:07:35', '1983-11-11 10:07:35');
INSERT INTO people (name, birthdate, birthtime, birthdt)
VALUES('Larry', '1943-12-25', '04:10:42', '1943-12-25 04:10:42');
SELECT * FROM people;
/*-----------------------------------*/
/*
Date time functions
CURDATE() - gives current date
CURTIME() - gives current time
NOW() - gives current datetime
Date time format
DAY()
DAYNAME()
DAYOFWEEK()
DAY OF YEAR()
*/
/*MM-dd-YY*/
select birthdate, DATE_FORMAT(birthdate,'%m-%d-%y at %h:%m') AS 'after formatted'
FROM people;
/*
# birthdate, after formatted
'2020-03-01', '03-01-20 at 12:03'
'1943-12-25', '12-25-43 at 12:12'
'1943-12-25', '12-25-43 at 12:12'
*/
SELECT birthdate, DATEDIFF(NOW(), birthdate) AS 'number of days between today and birthdate'
FROM people;
/*-----------------------------------*/
INSERT INTO people (name, birthdate, birthtime, birthdt)
VALUES('Richard', CURDATE(), CURTIME(), NOW());
SELECT birthdate, DAYOFYEAR('2020-03-01')
FROM people;
/*-----------------------------------*/
/*Date Time Specifier*/
/*Sunday October 2009*/
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
/*MM-dd-YY => '2020-03-01' => '03-01-20 at 12:03'*/
select birthdate, DATE_FORMAT(birthdate,'%m-%d-%y at %h:%m') AS 'after formatted'
FROM people;
/*-----------------------------------*/
/*
** DATE Math / Arithmetic **
DATEDIFF()
DATE_ADD()
+ or -
*/
/* result : 1 */
SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
/* '2020-03-01 19:28:23' => '2020-03-31 19:28:23'*/
SELECT birthdt, DATE_ADD(birthdt, INTERVAL 30 DAY) AS 'after adding 30days'
FROM people;
/*'2020-03-01 19:28:23' => '2020-03-11 19:28:23'*/
SELECT birthdt, birthdt + INTERVAL 10 DAY AS 'after adding 10days'
FROM people;
/*'2020-03-01 19:28:23' => '2020-02-01 19:28:23'*/
SELECT birthdt, birthdt - INTERVAL 1 MONTH AS 'after subtracting 1 month'
FROM people;
/*-------------------------------------*/
/*--- Timestamp -----------*/
CREATE TABLE comments(
comment VARCHAR(150) NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
change_at TIMESTAMP DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP
);
INSERT INTO comments(comment)
VALUES('I like cats'),
('today is pretty hot'),
('I can\'t stop watching movies :)');
UPDATE comments
SET comment = 'I like cats and dogs'
WHERE comment = 'I like cats';
/*-------------------------------------*/
/*-------------- Challenges -----------------------*/
/*What's a good use case for CHAR?
It can be used for text which we know in advance that it will have fixed length.
Example: gender (M/F) for CHAR(1)
*/
/*
Fill In The Blanks
CREATE TABLE inventory (
item_name ____________,
price ________________,
quantity _____________
);
(price is always < 1,000,000)
*/
CREATE TABLE inventory (
item_name VARCHAR(100),
price DECIMAL(8,2),
quantity INT
);
/*Print Out The Current Time*/
SELECT CURTIME();
/*Print Out The Current Date (but not time)*/
SELECT CURDATE();
/*Print Out The Current Day Of The Week (the number)*/
SELECT DAY(NOW());
SELECT DAYOFWEEK(CURDATE());
SELECT DAYOFWEEK(NOW());
SELECT DATE_FORMAT(NOW(), '%w') + 1;
/*Print Out The Current Day Of The Week (The Day Name)*/
SELECT DAYNAME(NOW());
SELECT DATE_FORMAT(NOW(), '%W');
/*Print out the current day and time using this format:mm/dd/yyyy*/
SELECT DATE_FORMAT(NOW(), '%m/%d/%Y');
SELECT DATE_FORMAT(CURDATE(), '%m/%d/%Y');
/*Print out the current day and time using this format:
January 2nd at 3:15 O/ April 1st at 10:18
https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php
*/
SELECT DATE_FORMAT(NOW(),'%M %D at %H:%m');
SELECT DATE_FORMAT(NOW(), '%M %D at %h:%i');
/*Create a tweets table that stores:
The Tweet content
A Username
Time it was created*/
CREATE TABLE tweets(
tweet VARCHAR(140) NOT NULL,
username VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);