The problem I have been provided is:
Write the DDL to construct the Facebook User and Post tables in SQL server. You need to populate the user table with the user’s information provided as in Figure 1 and additional attributes may be needed for the design. Use the ID in the excel file to identify the other Facebook user. For example ID 612831408762037 is David Cunliffe. Import the excel sheets to SQL server and then write effective SQL to merge the two tables into the Post table.
The code I have written so far is:
IF EXISTS(
SELECT *
FROM sys.tables
WHERE name = N'FacebookUser'
)
DROP TABLE FacebookUser;
IF EXISTS(
SELECT *
FROM sys.tables
WHERE name = N'Post'
)
DROP TABLE Post;
CREATE TABLE FacebookUser
(UserID varchar(15) primary key not null,
UserName varchar(30) not null,
UserDescript varchar(500),
JoinDate date not null,
HomeTown varchar(30),
Affiliation varchar(30),
Country varchar(30),
CurrentOffice varchar(100),
Gender varchar(6) not null,
Email varchar(30),
OtherAccounts varchar(100),
Website varchar(500),
);
CREATE TABLE Post
(id char(28) not null,
message varchar(8000) not null,
type varchar(15) not null,
created_time datetime not null,
updated_time datetime not null,
shares.count int not null,
count of likes.name int not null,
count of comments.message int not null,
Link varchar(50) not null,
name varchar(50) not null,
Description varchar(200) not null,
);
INSERT INTO FacebookUser
VALUES('612831408762037', 'Some Name', 'Some very long text to insert in my row just for test cases',
'02/18/2009', 'New Lynn/Auckland', 'New Zealand Labour Party', 'New Zealand', 'Office: Member of Parliament for New Lynn Party: New Zealand Labour Party', 'Male',
'Mailadress#example.com', 'Some Name (Other Service)', 'www.example.com');
INSERT INTO FacebookUser
VALUES('1119736203', 'Another Name', 'Some other example text, with some extend ', '02/20/2008', NULL,
NULL, 'New Zealand', 'Office: Prime Minister Party: NZ National Party', 'Male', 'a.someone#example.com', NULL,
'http://example.com');
I have imported the data, but am unsure how merge the data into the second table, and I get errors on these lines of code:
shares.count int not null,
Link varchar(50) not null,
name varchar(50) not null,
Description varchar(200) not null,
I'm completely lost from here. Is anybody able to help?
You will have to use square brackets for field names which are keywords or containing spaces or invalid characters
[shares.count] int not null,
[count of likes.name] int not null,
[count of comments.message] int not null,
In addition you will have to make sure that the fields are big enough to keep the values you want to store, other ways you will get errors like String or binary data would be truncated.
Related
I can use IDENTITY to generate unique ID numbers, but is there a way to generate a completely random number when inserting data? Currently, all ID's start with "1000"
Here is my original table:
CREATE TABLE Badges
(ID INTEGER IDENTITY(1000000000,23),
EmpID INTEGER NOT NULL,
EmpName nvarchar(50) not null,
EmpLastName nvarchar(50) not null,
IssueDate date default getdate(),
Validity char(1) default 'Y',
CONSTRAINT STATUS_CHECK check (Validity in ('Y','N')))
Below is my attempt to set up random ID number generation. I also tried to nest RAND into IDENTITY, but it did not work. Probably incorrect syntax, not so sure.
CREATE TABLE Badges
(Badge_ID INTEGER DEFAULT ROUND(RAND()*100000000,0) UNIQUE,
EmpID INTEGER NOT NULL,
EmpName nvarchar(50) not null,
EmpLastName nvarchar(50) not null,
IssueDate date default getdate(),
Validity char(1) default 'Y',
CONSTRAINT ID_status CHECK(Validity in ('Y','N')));
For a test, I tried inserting all employees from department 8 but now it generates the same Badge_ID number for everybody, at least if I insert employees in bulk and not one by one.
INSERT INTO Badges (EmpID, EmpName, EmpLastName)
SELECT EmployeeID, FirstName, LastName FROM HR.Employee WHERE DepartmentID = 8
So I tried to insert data in the ''customer'' table but I always get this certain error (at the bottom).
Can anyone pinpoint if I miss anything or if it's lacking?
create table rooms
(
roomid int identity(1,1) primary key,
roomNo varchar(250) not null unique,
roomType varchar(250) not null,
bed varchar(250) not null,
price bigint not null,
booked varchar(50) default 'NO'
);
insert into rooms (roomNo, roomType, bed, price) values (010, 'Studio with AC', 'Single', '1000');
create table customer
(
cid int identity(1,1) primary key,
cname varchar(250) not null,
mobile bigint not null,
nationality varchar(250) not null,
gender varchar(50) not null,
dob varchar(50) not null,
idproof varchar(50) not null,
addres varchar(250) not null,
checkIn varchar(250) not null,
checkOut varchar(250),
chekOut varchar(250) not null default 'NO',
roomid int foreign key references rooms(roomid)
);
insert into customer (cname,mobile,nationality,gender,dob,idproof,addres,checkIn,customer.roomid) values ('Kayano Ai', 09238400394, 'Japanese', 'Female', '01-02-1998', '8923giaf', 'Somewhere', 'YES', 20);
the error notif I got is
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__customer__roomid__1F98B2C1". The conflict occurred in database "myHotel", table "dbo.rooms", column 'roomid'.
When you enter row into your customer table, customer.roomid should exist in the rooms table first.
Fist make sure the room exists or insert a new room.
INSERT INTO rooms ( roomid,roomNo, ...) VALUES (1,'room1',...);
Then insert into customer with an custormer.roomid that exists in the room table (1 in the example)
INSERT INTO customer ( cid,cname,roomid ...) VALUES (15,'customer name',1,...);
this is my first hands-on experience with Sql and I'm trying to construct One-To-Many relationship, here is the SQL code:
create table car (
id SERIAL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19, 2) NOT NULL
);
create table person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
email VARCHAR(100),
date_of_birth DATE NOT NULL,
country_of_birth VARCHAR(50) NOT NULL,
cars_owning INT[] REFERENCES car(id)
);
and I get this error:
Key columns "cars_owning" and "id" are of incompatible types: integer[] and integer.
Here is the model a person can have many cars but each car have only one owner
typically (as a good general rule of thumb) the many refers to the one. not the other way around as you have tried.
So, alter the schema as:
create table car (
id SERIAL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19, 2) NOT NULL,
owner_id BIGINT FOREIGN KEY REFERENCES person(id)
);
create table person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
email VARCHAR(100),
date_of_birth DATE NOT NULL,
country_of_birth VARCHAR(50) NOT NULL
);
If you need to get the list of cars owned by a person, use this select statement
SELECT person.id, ARRAY_AGG(car.id) owns_car_ids
FROM person
LEFT JOIN car ON person.id = car.owner_id
GROUP BY 1
I have a table called Customers and Credit cards. I am trying to insert data into the customers table. I got confused how to insert data into customer and it throw an error saying
Column name or number of supplied values does not match table definition.
CreditCards table
CREATE TABLE CreditCards
(
CreditCardID INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(40) NOT NULL,
MiddleName NVARCHAR(40),
LastName NVARCHAR(40) NOT NULL,
Brand NVARCHAR(40) NOT NULL,
Bank NVARCHAR(40) NOT NULL,
[Number] INT UNIQUE NOT NULL,
DateofExpiration DATETIME NOT NULL,
VerificationCode INT NOT NULL
);
Customer table:
CREATE TABLE Customers
(
CustomerID INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(40) NOT NULL,
MiddleName NVARCHAR(40),
LastName NVARCHAR(40) NOT NULL,
StreetAddress NVARCHAR(100) NOT NULL,
[State] NVARCHAR(40) NOT NULL,
Country NVARCHAR(40) NOT NULL,
CellPhoneNumber NVARCHAR(40),
EmailAddress NVARCHAR(120),
CreditCardID INT FOREIGN KEY REFERENCES CreditCards(CreditCardID)
);
I am trying to insert data into customer table using this statement
INSERT INTO Customers
VALUES ('John', 'ba', 'Dock', '515 East Halliday Street', 'Idaho', 'United States of America', '2082203814', 'docjon#ss.org');
This error has nothing to do with foreign keys.
The Customers table has 10 columns, but you only have 8 values in your INSERT statement. You're missing the CustomerID and CreditCardID values.
You should list the columns that you're filling in explicitly in the INSERT statement:
INSERT INTO Customers (FirstName, MiddleName, LastNamr, StreetAddress, [State] , Country, CellPhoneNumber, EmailAddress)
VALUES('John','ba','Dock','515 East Halliday Street','Idaho','United State of America','2082203814','docjon#ss.org');
Columns that you leave out will get their default values; CustomerID will be filled in automatically using the IDENTITY feature, and CreditCardID will be NULL. If you want to use the CreditCardID of the row that was just inserted into the CreditCards table, you can use the ##IDENTITY variable:
NSERT INTO Customers (FirstName, MiddleName, LastNamr, StreetAddress, [State] , Country, CellPhoneNumber, EmailAddress, CreditCardID)
VALUES('John','ba','Dock','515 East Halliday Street','Idaho','United State of America','2082203814','docjon#ss.org', ##IDENTITY);
This is a good idea even when you're providing all columns, since you don't make your statement dependent on the order of the columns in the table creation. It also makes the code easier to understand, since readers don't have to refer back to the table specification to know what each value is.
BTW, you have a typo in one of your column names: LastNamr should presumably be LastName.
I am writing a database for a website for school, in Microsoft SQL Server Express
The database has a lot of different tables and also a lot of primary keys, it is not allowed to change the table itself.
The problems is that I have 2 tables, Verkoper & Gebruiker, (sorry the names are in dutch), these are the 2 tables:
create table Verkoper (
Gebruiker char(10) not null,
Bank char(8) ,
Bankrekening int,
Controle_Optie char(10) not null,
Creditcard char(19)
constraint pk_Verkoper primary key(Gebruiker),
constraint fk_Verkoper_Gebruiker foreign key(Gebruiker)
references Gebruiker(Gebruikersnaam)
)
create table gebruiker(
Gebruikersnaam char(10) not null,
Voornaam char(10) not null,
Achternaam char (15) not null ,
Adresregel1 char(25) not null,
AdresRegel2 char(25),
Postcode char(7) not null,
Plaatsnaam char(25) not null,
Land char(15) not null,
GeboorteDag char(10) not null,
Mailbox char(25) not null,
Wachtwoord char(15)not null,
Vraag int not null,
Antwoordtekst char(20) not null,
Verkoper char(4) not null,
constraint pk_gebruiker primary key(Gebruikersnaam),
constraint fk_Gebruiker_Vraag foreign key(Vraag)
references Vraag(Vraagnummer)
)
I wanna check if gebruiker.Verkoper = 'wel'(yes) or 'niet'(no). If gebruiker.verkoper = 'niet' then you should not be allowed to add it to the verkoper table.
So in short 2 tables gebruiker and verkoper if you wanna add a person to Verkoper, gebruiker.verkoper has to be true.
I already tried to make a function:
Create function dbo.checkVerkoper()
RETURNS int
AS BEGIN RETURN(
select count(*)
from Verkoper
inner join gebruiker
on verkoper.gebruiker = gebruiker.gebruikersnaam
AND gebruiker.verkoper = 'wel'
)
END
go
alter table verkoper
add constraint ck_VerkoperGebruiker
check(dbo.checkVerkoper () = 1 )
This does work for 1 person, but if you add more the value will be more then 1, and so the check will be false no one will be add, if you have 4 persons and 3 are not allowed and 1 is then the value is still 1 and you can still add someone to verkoper tabel.
You can write a stored Procedure
In that
Check whether your conditions are met
if met, then Do the insert(s)