소스 검색

updated and committed.

L. Dolio Durant 6 년 전
커밋
76a06242c8
8개의 변경된 파일518개의 추가작업 그리고 0개의 파일을 삭제
  1. 28
    0
      .gitignore
  2. 340
    0
      README.md
  3. 21
    0
      data-h2.sql
  4. 62
    0
      pom.xml
  5. 21
    0
      src/main/java/io/zipcoder/tcuksql/SpringStarter.java
  6. 4
    0
      src/main/resources/application-h2.properties
  7. 3
    0
      src/main/resources/application.properties
  8. 39
    0
      src/main/resources/schema-h2.sql

+ 28
- 0
.gitignore 파일 보기

@@ -0,0 +1,28 @@
1
+target/
2
+.mvn/
3
+
4
+### STS ###
5
+.apt_generated
6
+.classpath
7
+.factorypath
8
+.project
9
+.settings
10
+.springBeans
11
+
12
+### IntelliJ IDEA ###
13
+.idea
14
+*.iws
15
+*.iml
16
+*.ipr
17
+
18
+### NetBeans ###
19
+nbproject/private/
20
+build/
21
+nbbuild/
22
+dist/
23
+nbdist/
24
+.nb-gradle/
25
+
26
+### OSX Files ###
27
+.DS_Store
28
+

+ 340
- 0
README.md 파일 보기

@@ -0,0 +1,340 @@
1
+# SQL
2
+
3
+The following lab is to be completed using MySQL in your terminal. 
4
+
5
+To begin import the schema for all of the tables from the following location: 
6
+
7
+`src/main/resources/schema-h2.sql` 
8
+
9
+You must create a database in which to import the schema by accessing mysql through the terminal, then exit and import the schema using: 
10
+
11
+`mysql -u root -p [database] < src/main/resources/schema-h2.sql` 
12
+
13
+Don't submit any changes to the schema file.  And, in all reality, you shouldn't be doing anything in there outside of
14
+experimenting for your own curiosity.
15
+
16
+## Guided Walkthrough
17
+
18
+Proceed through the sections below, testing out the queries shown and observing the result. 
19
+Feel free to experiment with your own variations on these queries. 
20
+This will help to build your familiarity with the process of working with SQL queries.
21
+
22
+#### Insert people into People table
23
+
24
+```SQL
25
+INSERT INTO people (last_name, first_name, mobile, birthday)
26
+VALUES ('Smith', 'John', '230-4293', '1973-01-23');
27
+```
28
+
29
+After doing that insert, go ahead and run all the statements in the `data-h2.sql` file.  That should build out your
30
+database enough to get started.  Feel free to add more people, though.
31
+
32
+My suggestion for the following stuff is, in the H2 console, leave the first line as a `SELECT *` from whatever tables
33
+you're querying.  Then, after that, run your queries.  That way you can compare the results of your query with the 
34
+data in the table.
35
+
36
+#### Selecting all rows from table 
37
+
38
+```SQL
39
+SELECT * FROM people;
40
+```
41
+
42
+#### Updating rows
43
+
44
+Update firstname for person whose id is 1
45
+
46
+```SQL
47
+UPDATE people SET first_name = 'Tony' WHERE id = 1;
48
+```
49
+
50
+Update mobile where last names are Smith
51
+
52
+```SQL
53
+UPDATE people SET mobile = '152-9854' WHERE last_name = 'Smith';
54
+```
55
+
56
+Update multiple columns with multiple conditions
57
+
58
+```SQL
59
+UPDATE people SET birthday = '1955-01-25' 
60
+WHERE last_name = 'Smith' 
61
+AND id = 4;
62
+```
63
+
64
+```SQL
65
+UPDATE people SET mobile = '333-3333', last_name = 'Johnson' 
66
+WHERE first_name = 'Noelle' OR first_name = 'Raj';
67
+```
68
+
69
+#### Basic Functions
70
+
71
+```SQL
72
+SELECT * FROM people;
73
+```
74
+
75
+```SQL
76
+SELECT COUNT(homenumber) FROM homes;
77
+```
78
+
79
+```SQL
80
+SELECT homenumber FROM homes WHERE id = 1;
81
+```
82
+
83
+```SQL
84
+SELECT COUNT(*) FROM homes;
85
+```
86
+
87
+```SQL
88
+SELECT COUNT(DISTINCT last_name) FROM people;
89
+```
90
+
91
+```SQL
92
+SELECT  SUM(id), AVG(id) FROM people;
93
+```
94
+
95
+```SQL
96
+SELECT SUM(id) AS sum, AVG(id) AS avg FROM people;
97
+```
98
+Notice the difference in the returned table with the aliases?
99
+
100
+```SQL
101
+SELECT MIN(birthday) FROM people;
102
+```
103
+
104
+#### Strings
105
+
106
+```SQL
107
+SELECT UPPER (first_name), LOWER(last_name) FROM people;
108
+```
109
+
110
+```SQL
111
+SELECT REPLACE(last_name, 'a', '1') FROM people;
112
+```
113
+
114
+```SQL
115
+SELECT last_name FROM people;
116
+```
117
+
118
+```SQL
119
+INSERT INTO people (first_name, last_name, mobile) 
120
+VALUES ('Otto', 'Von Count', '656-6548');
121
+```
122
+
123
+```SQL
124
+SELECT CONCAT(first_name, last_name) FROM people
125
+WHERE last_name = 'Smith';
126
+```
127
+
128
+```SQL
129
+SELECT CONCAT(first_name, ' ', last_name) 
130
+FROM people 
131
+WHERE last_name = 'Smith';
132
+```
133
+
134
+```SQL
135
+SELECT CONCAT_WS(' ',first_name, last_name, mobile) 
136
+FROM people WHERE last_name= 'Smith';
137
+```
138
+
139
+```SQL
140
+SELECT homenumber, LEFT(homenumber, 3), RIGHT(homenumber, 2) FROM homes;
141
+```
142
+
143
+```SQL
144
+SELECT LENGTH(address), CHAR_LENGTH(address) FROM homes;
145
+```
146
+
147
+#### Compare
148
+
149
+```SQL
150
+SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
151
+```
152
+
153
+```SQL
154
+SELECT first_name, birthday FROM people WHERE first_name='Thomas' OR first_name='Raj' OR first_name='Sheeri';
155
+```
156
+
157
+```SQL
158
+SELECT first_name, birthday FROM people WHERE first_name IN ('Noelle', 'Thomas', 'Raj');
159
+```
160
+
161
+#### Wild Cards
162
+
163
+```SQL
164
+SELECT first_name FROM people WHERE RIGHT(first_name,1)='e';
165
+```
166
+
167
+```SQL
168
+SELECT first_name FROM people WHERE first_name LIKE '%j'; 
169
+```
170
+
171
+```SQL
172
+SELECT first_name FROM people WHERE first_name LIKE '%o%';
173
+```
174
+
175
+```SQL
176
+SELECT first_name FROM people WHERE first_name NOT LIKE '%o%';
177
+```
178
+
179
+```SQL
180
+SELECT COUNT(*) FROM people;
181
+```
182
+
183
+```SQL
184
+SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
185
+```
186
+
187
+```SQL
188
+SELECT last_name, GROUP_CONCAT(mobile) FROM people GROUP BY last_name;
189
+```
190
+
191
+```SQL
192
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name;
193
+```
194
+
195
+```SQL
196
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name  HAVING COUNT(*)>1;
197
+```
198
+
199
+```SQL
200
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE last_name != 'Cabral' GROUP BY last_name  HAVING COUNT(*)>1;
201
+```
202
+
203
+#### Sorting 
204
+
205
+```SQL
206
+SELECT first_name, birthday FROM people ORDER BY birthday;
207
+```
208
+
209
+```SQL
210
+SELECT first_name, birthday FROM people ORDER BY birthday DESC;
211
+```
212
+
213
+```SQL
214
+SELECT first_name, last_name FROM people ORDER BY last_name, first_name;
215
+```
216
+
217
+```SQL
218
+SELECT first_name, birthday FROM people ORDER BY birthday DESC LIMIT 3;
219
+```
220
+
221
+```SQL
222
+SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
223
+```
224
+
225
+```SQL
226
+SELECT last_name, COUNT(*) FROM  people GROUP BY last_name;
227
+```
228
+
229
+```SQL
230
+SELECT last_name, COUNT(*) FROM  people GROUP BY last_name ORDER BY NULL;
231
+```
232
+
233
+#### Inserting and Replacing Records
234
+
235
+```SQL
236
+INSERT INTO people (first_name, last_name, birthday, home_id)
237
+VALUES ('John', 'Smith', '1998-04-07', 4),
238
+('Maya', 'Wasserman' , NULL, 4),
239
+('Paul', 'Thompson', '1996-05-27', 1);
240
+```
241
+
242
+#### Deleting
243
+
244
+```SQL
245
+DELETE FROM people WHERE first_name='Maya';
246
+```
247
+
248
+```SQL
249
+SELECT * FROM people;
250
+```
251
+
252
+#### JOIN
253
+
254
+```SQL
255
+INSERT INTO people (first_name, last_name, birthday)
256
+VALUES ('Eli', 'Kramer', '1984-01-15');
257
+```
258
+	
259
+	
260
+```SQL
261
+SELECT * FROM people;
262
+```
263
+
264
+```SQL
265
+SELECT * FROM homes;
266
+```
267
+
268
+```SQL
269
+SELECT p.first_name, h.address 
270
+FROM people p
271
+INNER JOIN homes h on (p.home_id = h.id);
272
+```
273
+
274
+```SQL
275
+SELECT first_name, last_name
276
+FROM people p
277
+INNER JOIN homes h on (p.home_id = h.id)
278
+WHERE p.HOME_ID = 1;
279
+```
280
+
281
+```SQL
282
+SELECT p.*, h.address, h.homenumber
283
+FROM people p
284
+INNER JOIN homes h  on (p.home_id = h.id)
285
+WHERE p.first_name  LIKE '%e%';
286
+```
287
+
288
+##### Exercise:
289
+
290
+Devise a report:
291
+
292
+Show all the people in your address table, only if you know their birthday.
293
+
294
+Show their name, address and birthday ordered by birthday month, so January birthdays are first.
295
+
296
+Devise a report:
297
+
298
+Output all information for all people and their home information
299
+
300
+
301
+## Mini Movie Database
302
+
303
+Once you have figured out the correct queries for each step, 
304
+save a copy in a file called `src/main/resources/script.sql`. This will be how you submit this assignment. 
305
+If at any time you need to reset the database, you can restart your Spring Boot server.
306
+
307
+Add the following movies to the `movies` table using an insert statement:
308
+
309
+| Title | Runtime | Genre | IMDB Score | Rating |
310
+| ----- | ------- | ----- | ----------- | ----- |
311
+| Howard the Duck | 110 | Sci-Fi | 4.6 | PG |
312
+| Lavalantula | 83 | Horror | 4.7 | TV-14 |
313
+| Starship Troopers | 129 | Sci-Fi | 7.2 | PG-13 |
314
+| Waltz With Bashir | 90 | Documentary | 8.0 | R |
315
+| Spaceballs | 96 | Comedy | 7.1 | PG |
316
+| Monsters Inc. | 92 | Animation | 8.1 | G |
317
+
318
+Add a few more movies of your choosing.
319
+
320
+Create a query to find all movies in the Sci-Fi genre.
321
+
322
+Create a query to find all films that scored at least a 6.5 on IMDB
323
+
324
+For parents who have young kids, but who don't want to sit through long children's movies, 
325
+create a query to find all of the movies rated G or PG that are less than 100 minutes long.
326
+
327
+Create a query to show the average runtimes of movies scoring below a 7.5 on imdb, grouped by their respective genres.
328
+
329
+There's been a data entry mistake; Starship Troopers is actually rated R, not PG-13. 
330
+Create a query that finds the movie by its title and changes its rating to R.
331
+
332
+Show the ID number and rating of all of the Horror and Documentary movies in the database. Do this in only one query.
333
+
334
+This time let's find the average, maximum, and minimum IMDB score for movies of each rating.
335
+
336
+That last query isn't very informative for ratings that only have 1 entry. 
337
+Use a `HAVING COUNT(*) > 1` clause to only show ratings with multiple movies showing.
338
+
339
+Let's make our movie list more child-friendly. Delete all entries that have a rating of R. 
340
+Remember to record your query in `script.sql`.

+ 21
- 0
data-h2.sql 파일 보기

@@ -0,0 +1,21 @@
1
+
2
+INSERT INTO homes (address, homenumber) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895');
3
+INSERT INTO homes (address, homenumber) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544');
4
+INSERT INTO homes (address, homenumber) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515');
5
+INSERT INTO homes (address, homenumber) VALUES ('234 High Street, PA 19159 ', '267-3940');
6
+
7
+
8
+INSERT INTO people (last_name, first_name, mobile, birthday, home_id)
9
+VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2);
10
+INSERT INTO people (last_name, first_name, mobile, birthday, home_id)
11
+VALUES ('Sharam', 'Raj', '186-5223', '1980-08-31', 3);
12
+INSERT INTO people (last_name, first_name, mobile, birthday, home_id)
13
+VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1);
14
+INSERT INTO people (last_name, first_name, mobile, birthday, home_id)
15
+VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1);
16
+INSERT INTO people (last_name, first_name, mobile, birthday, home_id)
17
+VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3);
18
+INSERT INTO people (last_name, first_name, mobile, birthday, home_id)
19
+VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3);
20
+
21
+

+ 62
- 0
pom.xml 파일 보기

@@ -0,0 +1,62 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>io.zipcoder</groupId>
8
+    <artifactId>TCUK-SQL</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+
11
+    <parent>
12
+        <groupId>org.springframework.boot</groupId>
13
+        <artifactId>spring-boot-starter-parent</artifactId>
14
+        <version>1.5.3.RELEASE</version>
15
+        <relativePath/>
16
+    </parent>
17
+
18
+    <properties>
19
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21
+        <java.version>1.8</java.version>
22
+    </properties>
23
+
24
+    <dependencies>
25
+        <dependency>
26
+            <groupId>org.springframework.boot</groupId>
27
+            <artifactId>spring-boot-starter-jdbc</artifactId>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>org.springframework.boot</groupId>
31
+            <artifactId>spring-boot-starter-web</artifactId>
32
+        </dependency>
33
+
34
+        <dependency>
35
+            <groupId>com.h2database</groupId>
36
+            <artifactId>h2</artifactId>
37
+            <scope>compile</scope>
38
+        </dependency>
39
+        <dependency>
40
+            <groupId>org.springframework.boot</groupId>
41
+            <artifactId>spring-boot-starter-test</artifactId>
42
+            <scope>test</scope>
43
+        </dependency>
44
+        <!-- https://mvnrepository.com/artifact/com.atlassian.clover/clover -->
45
+        <dependency>
46
+            <groupId>com.atlassian.clover</groupId>
47
+            <artifactId>clover</artifactId>
48
+            <version>4.0.6</version>
49
+        </dependency>
50
+
51
+    </dependencies>
52
+
53
+    <build>
54
+        <plugins>
55
+            <plugin>
56
+                <groupId>org.springframework.boot</groupId>
57
+                <artifactId>spring-boot-maven-plugin</artifactId>
58
+            </plugin>
59
+        </plugins>
60
+    </build>
61
+
62
+</project>

+ 21
- 0
src/main/java/io/zipcoder/tcuksql/SpringStarter.java 파일 보기

@@ -0,0 +1,21 @@
1
+package io.zipcoder.tcuksql;
2
+
3
+import org.h2.server.web.WebServlet;
4
+import org.springframework.boot.SpringApplication;
5
+import org.springframework.boot.autoconfigure.SpringBootApplication;
6
+import org.springframework.boot.web.servlet.ServletRegistrationBean;
7
+import org.springframework.context.annotation.Bean;
8
+
9
+@SpringBootApplication
10
+public class SpringStarter {
11
+    public static void main(String[] args){
12
+        SpringApplication.run(SpringStarter.class, args);
13
+    }
14
+
15
+    @Bean
16
+    ServletRegistrationBean h2servletRegistration() {
17
+        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
18
+        registrationBean.addUrlMappings("/console/*");
19
+        return registrationBean;
20
+    }
21
+}

+ 4
- 0
src/main/resources/application-h2.properties 파일 보기

@@ -0,0 +1,4 @@
1
+spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
2
+spring.datasource.platform=h2
3
+spring.jpa.hibernate.ddl-auto=none
4
+spring.datasource.continue-on-error=true

+ 3
- 0
src/main/resources/application.properties 파일 보기

@@ -0,0 +1,3 @@
1
+spring.profiles.active=h2
2
+logging.level.org.springframework.boot.context.embedded=INFO
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

+ 39
- 0
src/main/resources/schema-h2.sql 파일 보기

@@ -0,0 +1,39 @@
1
+DROP TABLE IF EXISTS people;
2
+
3
+CREATE TABLE people (
4
+  id INT NOT NULL AUTO_INCREMENT,
5
+  first_name VARCHAR2(255) NOT NULL DEFAULT '',
6
+  last_name VARCHAR2(255) NOT NULL DEFAULT '',
7
+  mobile VARCHAR2(20),
8
+  birthday DATE DEFAULT NULL,
9
+  home_id SMALLINT DEFAULT NULL,
10
+  PRIMARY KEY (id));
11
+
12
+  ALTER TABLE people
13
+  ADD FOREIGN KEY (home_id)
14
+  REFERENCES homes(id);
15
+
16
+
17
+DROP TABLE IF EXISTS homes;
18
+
19
+CREATE TABLE homes (
20
+  id INT NOT NULL AUTO_INCREMENT,
21
+  address VARCHAR2(255) NOT NULL DEFAULT '',
22
+  homenumber VARCHAR2(255) NOT NULL DEFAULT '',
23
+  PRIMARY KEY (id)
24
+);
25
+
26
+DROP TABLE IF EXISTS movies;
27
+
28
+CREATE TABLE movies (
29
+  id INT PRIMARY KEY AUTO_INCREMENT,
30
+  title VARCHAR2(100) NOT NULL UNIQUE,
31
+  runtime SMALLINT NOT NULL,
32
+  genre VARCHAR2(50),
33
+  imdb_score NUMBER(10,1),
34
+  rating VARCHAR(10)
35
+);
36
+
37
+DROP SEQUENCE hibernate_sequence;
38
+
39
+CREATE SEQUENCE hibernate_sequence;