简体   繁体   中英

Creating a table in SQL using Oracle 11G

I am new to learning SQL and have been struggling to create a table for an assignment. These are the requirements:

Create a new table to track the Library location.

  • LIBRARY (lib_id, lib_name, lib_address, lib_city, lib_state, lib_zip)

  • LIB_ID is the library id – it is an auto generated number. (you should create a sequence number called lib_id_seq, start with 1001 and increment by 1.)

  • LIB_ID is the primary key.
  • LIB_NAME, LIB_ADDRESS, and LIB_CITY is between 1 and 35 characters.
  • LIB_STATE is 2 characters – default to TX.
  • LIB_ZIP is 5 numbers. Check for one of the following zip codes – 75081, 75080, 75082, 75079, 75078

And this is what I have written out so far:

CREATE TABLE LIBRARY
(
  LIB_ID INT(4),
  LIB_ADDRESS VARCHAR(35),
  LIB_CITY VARCHAR(35),
  LIB_STATE VARCHAR(2) DEFAULT ‘TX’,
  LIB_ZIP INT(5) CHECK (Frequency IN ('75078', ‘75079', '75080', '75081', ‘75082’))
  PRIMARY KEY(LIB_ID)
);

CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;

I keep getting errors, but am not sure what I need to fix.

For oracle (Kid Tested unsure if SO approved)...

  • use varchar2 instead of varchar
  • use Number instead of int
  • added constraint syntax (named them)
  • adjusted apostrophe's (Removed) instead of whatever the heck you had in some of them :P (It's a numeric field shouldn't be using text apostrophes!)
  • personally I wouldn't name a table library as that's a reserved word
  • I woudln't use a numeric Zip code as we will never do math on a zipcode.

. .

CREATE TABLE LIBRARY (
LIB_ID Number(4),
LIB_ADDRESS VARCHAR2(35),
LIB_CITY VARCHAR2(35),
LIB_STATE VARCHAR2(2) DEFAULT 'TX',
LIB_ZIP NUMBER(5),
CONSTRAINT Lib_ZIP_CON CHECK (LIB_ZIP IN (75078, 75079, 75080, 75081, 75082)),
CONSTRAINT LIB_ID_PK PRIMARY KEY(LIB_ID)
);

CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;

在此处输入图片说明

This works for SQL Server. You need to modify the syntax accordingly for the concerned db.

CREATE TABLE LIBRARY

(
LIB_ID INTEGER PRIMARY KEY,

LIB_ADDRESS VARCHAR(35),

LIB_CITY VARCHAR(35),

LIB_STATE VARCHAR(2) DEFAULT 'TX',

LIB_ZIP INTEGER, 

CHECK( LIB_ZIP IN ('75078', '75079', '75080', '75081', '75082') )

);


CREATE SEQUENCE LIB_ID_SEQ

START WITH 1001

INCREMENT BY 1;

For learning how to create tables and constraints check this link on w3schools as you seem to be a beginner.

http://www.w3schools.com/sql/sql_primarykey.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM