SQL Server 2014 : help creating tables - sql

I am new to MSSQL 2014 Server, my professor listed these steps to make a table, I don't know the proper steps to create tables in the pictures listed below, please help.
Create and populate (insert values) the following tables per table description and data values provided
DEPARTMENT
EMPLOYEE
PROJECT
ASSIGNMENT
Add a SQL Comment to include /* * Your First Name_Your Last Name* */ when inserting corresponding values for each table.
What I tried so far:
CREATE TABLE DEPARTMENT(
DepartmentName Text(35) PRIMARY KEY,
BudgetCode Text(30) NOT NULL,
OfficeNumber Text(15) NOT NULL,
Phone Text(12) NOT NULL, );
I have put this to my query and the error is
Msg 2716, Level 16, State 1, Line 1 Column, parameter, or variable #1: Cannot specify a column width on data type text.

Try this(I assume that your table exists in dbo schema):
IF OBJECT_ID(N'dbo.DEPARTMENT', N'U') IS NOT NULL
BEGIN
DROP TABLE DEPARTMENT
END
GO
CREATE TABLE DEPARTMENT(
DepartmentName varchar(35) PRIMARY KEY,
BudgetCode varchar(30) NOT NULL,
OfficeNumber varchar(15) NOT NULL,
Phone varchar(12) NOT NULL
);
You can not define width for Text data type. In case which you need to define width you can use char or varchar data types. Also keep in mind that if you need to work with Unicode characters then you will need to use nchar or nvarchar instead.

Related

DBgrid column very wide

We have the following case. We need to change a field in a SQL Server database from varchar to nvarchar. After the change, all the dbgrid shows the very wide column. How can we globally adjust the size of this column?
Column Lastname is very wide:
Let's create a sample table:
CREATE TABLE PATIENT(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
LastName NVARCHAR(50) NOT NULL
)
GO
INSERT INTO PATIENT VALUES
('Patient1'),
('Patient2'),
('Patient3');
Now let's see the DBGrid how to show the data:
Your problem is the Sise of your column (50) here
You can change the DBGrid column Width as:
DBGrid1.Columns[1].Width := Value;
also, be sure that dgColumnResize option of the DBGrid is enabled (true), in that way you resize the column as needed at runtime.

SQL server Invalid Column name Invalid object name

I'm having a problem with a table I created. I am trying to run a query however a red line appears under my code ('excursionID', and 'excursions'), claiming 'Invalid Column name 'excursionID' and 'Invalid object name 'dbo.excursions' even though I have created the table already!
Here is the query
SELECT
excursionID
FROM [dbo].[excursions]
Here is the query I used to create the table
USE [zachtravelagency]
CREATE TABLE excursions (
[excursionID] INTEGER NOT NULL IDENTITY (1,1) PRIMARY KEY,
[companyName] NVARCHAR (30) NOT NULL,
[location] NVARCHAR (30) NOT NULL,
[description] NVARCHAR (30) NOT NULL,
[date] DATE NOT NULL,
[totalCost] DECIMAL NOT NULL,
I've tried dropping the table and inserting table again.
For some reason all my other tables work, it's just this table that doesn't identify itself. I'm very new to SQL so thank you for your patience!
You use DB [zachtravelagency] for create table.And You dont use this DB in your query. Default used db master in SSMS. Try
SELECT
excursionID
FROM [zachtravelagency].[excursions]

SQL constraint “at least one of two attributes”

I need to create a table User with telephone_number and e_mail_adress columns. Each row must have at least one of those columns set. It could have both or just one, but it must have at least one of them.
How can I express that constraint in SQL?
create table Users (
/* Whatever */
TelephoneNumber varchar(2000) null,
EmailAddress varchar(5) null,
constraint CK_AtLeastOneContact CHECK (
TelephoneNumber is not null or
EmailAddress is not null
)
)
You may want to adjust the data types :-)

oracle error: not enough values

i have a table donor_master:
create table donor_master
(
donor_id number(10) primary key not null,
dob date not null,
age number(3) not null,
gender char(1) not null,
blood_group char(3),
contact_no number(10),
address varchar(50) not null,
city varchar(10) not null,
pin number(10) not null,
state varchar(10) not null,
branch_registration_id number(5) references branch_master(branch_id)
);
when i try to insert into the table in a procedure insert_donor_master, i get "not enough values" error on compilation.
this is the procedure:
create or replace procedure insert_donor_master(
vdob donor_master.dob%type,
vage donor_master.age%type,
vgender donor_master.gender%type,
vblood_group donor_master.blood_group%type,
vcontact_no donor_master.contact_no%type,
vaddress donor_master.address%type,
vcity donor_master.city%type,
vpin donor_master.pin%type,
vstate donor_master.state%type,
vbranch_registration_id donor_master.branch_registration_id%type
)
is
begin
insert into donor_master values (sq_donor_master.nextval, vdob, vage, vgender, vblood_group, vcontact_no, vaddress, vcity, vpin, vstate, vbranch_registration_id);
commit;
end;
What is the problem?
Thanks.
Oracle hurls ORA-00947 when we specify an INSERT statement which doesn't have a value for every column in the table.
Now, the CREATE TABLE statement you posted shows a table with eleven columns. And the stored procedure code you posted shows an insert statement with eleven values in the VALUES (...) clause.
So, the explanations are:
you have a configuration management issue, and you're running the wrong version of the stored procedure or the wrong version of the table
you have a configuration management issue, and the actual structure of the table isn't what you think it is (doesn't match your CREATE TABLE script)
you aren't really getting an ORA-00947 error
Note that if you don't want to populate every row you can specify a projection of the relevant columns before the VALUES clause. For instance, if you just wanted to populate the mandatory columns you would code this:
insert into donor_master
(donor_id, dob, age, gender, address, city, pin, state )
values (sq_donor_master.nextval, vdob, vage, vgender, vaddress, vcity, vpin, vstate)
All that matters is that the number of values matches the number of columns.
The complete syntax for INSERT statements is in the documentation. enter link description hereFind out more.

Selecting from a table and inserting into another table's column of a different type using query in ms access

I have some txt files that contain tables with a mix of different records on them which have diferent types of values and definitons for columns. I was thinking of importing it into a table and running a query to separate the different record types since a identifier to this is listed in the first column. Is there a way to change the value type of a column in a query? since it will be a pain to treat all of them as text. If you have any other suggestions on how to solve this please let me know as well.
Here is an example of tables for 2 record types provided by the website where I got the data from
create table dbo.PUBACC_A2
(
Record_Type char(2) null,
unique_system_identifier numeric(9,0) not null,
ULS_File_Number char(14) null,
EBF_Number varchar(30) null,
spectrum_manager_leasing char(1) null,
defacto_transfer_leasing char(1) null,
new_spectrum_leasing char(1) null,
spectrum_subleasing char(1) null,
xfer_control_lessee char(1) null,
revision_spectrum_lease char(1) null,
assignment_spectrum_lease char(1) null,
pfr_status char(1) null
)
go
create table dbo.PUBACC_AC
(
record_type char(2) null,
unique_system_identifier numeric(9,0) not null,
uls_file_number char(14) null,
ebf_number varchar(30) null,
call_sign char(10) null,
aircraft_count int null,
type_of_carrier char(1) null,
portable_indicator char(1) null,
fleet_indicator char(1) null,
n_number char(10) null
)
Yes, you can do what you want. In ms access you can use any VBA functions and with some
IIF(FirstColumn="value1", CDate(SecondColumn), NULL) as DateValue,
IIF(FirstColumn="value2", CDec(SecondColumn), NULL) as DecimalValue,
IIF(FirstColumn="value3", CStr(SecondColumn), NULL) as StringValue
You can use all/any of the above in your SELECT.
EDIT:
From your comments it seems that you want to split them into different tables - importing as text should not be a problem in that case.
a)
After you import and get it in the initial table, create the proper table manually setting you can INSERT into the proper table.
b)
You could even do a make table query, but it might be faster to create it manually. If you do a make table query you have to be sure that you have casted the data into proper type in your select.
EDIT2:
As you updated the question showing the structure it becomes obvious that my suggestion above will not help directly.
If this is one time process you can follow HLGEM's solution. Here are some more details.
1) Import into a table with two columns - RecordType char(2), Rest memo
2) Now you can split the data (make two queries that select based on RecordType) and re-export the data (to be able to use access' import wizard)
3) Now you have two text files with proper structure which can be easily imported
I did this in my last job. You start with a staging table that has one column or two coulmns if your identifier is always the same length.
Then using the record identifier, you move the data to another set of staging tables, one for each type of record you have. This will be in columns for the data and can have the correct data types. Then you do any data cleaning you need to do. Then you insert into the real production table.
If you have a column defined as text, because it has both alphas and numbers, you'll only be able to query it as if it were text. Once you've separated out the different "types" of data into their own tables, you should be able to change the schema definition. Please comment here if I'm misunderstanding what you're trying to do.