Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implementation/SQL Introduction: An assignment to make sure you get Postgres installed and can execute basic statements. The subject of the table you will work with
Implementation/SQL
Introduction:
An assignment to make sure you get Postgres installed and can execute basic statements. The subject of the table you will work with is our local cities and people. We will start with a single table and modify this in the weeks to come.
The assignment:
- Download and install Postgres.
- In pgAdmin 4, on the left, explode the Servers to choose one, after which you should see the Databases item. Right-click on that item to create a database or use the default database called postgres
- If you right-click on the database, the next-to-last menu option is Query Tool. Choose that and then you can save your work in a script called create.sql to define the table city.
- Remember that SQL is code, even if the syntax is new for you. Do not use Word or another word processor to edit it. The Query Tool feature is not the greatest, but it has syntax highlighting.
- When saving your files, remember to add .sql at the end the pgAdmin editor does not do this automatically
- Execute your scripts to test them.
- To distinguish assignments for grading purposes, create a schema named using your last name (up to 6 letters), appending your first initial, so my schema would be yangd
- You can find examples at: PostgreSQL: Documentation: 14: CREATE SCHEMA
- This general section of the Postgres documentation has all the SQL syntax, so its a decent bookmark to have for this semester
- Define the table within the schema for me, this would be yangd.city
- The fields are:
- id : a SERIAL field city names are far from unique
- name : a string for the name of the city
- county : a string for the name of the county
- state : a string for the name of the state
- incorporated: date on which the city was officially established
- Remember that SQL is code, even if the syntax is new for you. Do not use Word or another word processor to edit it. The Query Tool feature is not the greatest, but it has syntax highlighting.
- Create a script insert_ok.sql to insert acceptable data. Use INSERT statements to insert records into the city table
- Insert enough records so that any queries beyond the first one will return at least one, but not all records
- You may find PostgreSQL: Documentation: 14: 9.8. Data Type Formatting Functions a useful resource for the incorporated attribute just call the function instead of a literal value
- SQL itself does not have the kind of variables like Python or C++ does, so do not try to separate this into multiple statements
- [Databases like Postgres do support stored procedures for more complicated operations, but that is a separate language]
- Create a script query.sql that includes semicolon-separated queries to do the following (terminate each query with a semicolon so the grader can run the whole script at once) :
- List name, county and state of all cities
- List the DISTINCT counties of California in your database (of course, you are not expected to insert data for all counties, but you should include at least two California counties, at least one county outside of California, and at least two cities in one of the California counties). No hard-coding write the query against the data you insert.
- List how many years old (rounded down) Hayward is again, you only get credit for writing the query against your data, not for hard-coding a response. You should find the age() and date_trunc() functions useful. See PostgreSQL: Documentation: 14: 9.9. Date/Time Functions and Operators for examples
- Create a script insert_bad.sql that uses INSERT statements to insert data that should not be allowed, violating the rules below. We will eventually cover the various features of SQL that allow you to block these inserts, but for now, they should be allowed
- City name plus county plus state should be unique (Note that city name by itself is not unique, even within a single state)
- The date of incorporation cannot be in the future
- The name of the state cannot be null
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started