SQL DROP TABLE FUNCTION - sql

Hello i've created a table if in a VM provided by my university and inserted the following:
create table first_table (record_id int primary key,
first_name varchar(20) not null,
last_name varchar(20) not null);
However, instead of the first_name and last_name I actually inserted my name and am looking to drop the table to recreate it.
" I typed in DROP TABLE [IF EXISTS] first_table "
and then it simply doesn't do anything. Any idea why.

In the Postgresql documentation, when you see something inside square brackets such as [IF EXISTS] it means that "IF EXISTS" is optional. You shouldn't type the square brackets if you put that in.
In this case, though, you know that the table exists so just leave the "IF EXISTS" part off:
drop table first_table;
Given that the problem is that you typed your first and last names instead of the desired column name (first_name and last_name) you can simply rename the columns using
alter table first_table
rename leo as first_name
alter table first_table
rename whatever_your_last_name_is as last_name

Try this:
DROP TABLE first_table;

In PSQL you should be able to do so using the command
DROP TABLE first_table;

Related

SQL constraint E-Mail

i have an assignment that i have to do in SQL Server Manager.
I have a database, and a table named User. Under user there are a column named e-mail.
The assignment is to create a new constraint to that table that affect the column e-mail.
There has to be only one '#' and minimum one '.'
It is not allowed to be any special characters such as ( !, ", #, ¤, %, etc.) <- this do not include the '#' and '.'
I've tried some different things but cant seem to make it work. Also it should be noticed that I am a beginner.
Thanks for your help
Alternatively you can create your table with constraint, like this which will check if all the given conditions satisfies.
Please change table data with your's
If table not Exist
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Email varchar(255),
CHECK (len(Email) - len(replace(Email,'#',''))=1 AND len(Email) -
len(replace(Email,'.',''))=1 AND CHARINDEX('!',Email)!>0 AND
CHARINDEX('#',Email)!>0 AND CHARINDEX('%',Email)!>0 AND
CHARINDEX('¤',Email)!>0 AND CHARINDEX('"',Email)!>0)
);
Some Rejected Inputs
some#some.!%#¤com
some#some.!%#com
some#some.%#com
some#some.#com
some#some#com
!some#some#com
etc...
Some Excepted Inputs
some#some.com
some222#some.com
harry_porter#some.com
332#some.com
etc....
If you have already a table then
GO
ALTER TABLE [dbo].[Yourtablename] WITH CHECK ADD Constraint EmailConstraint CHECK (((len([Email])-
len(replace([Email],'#','')))=(1) AND (len([Email])-
len(replace([Email],'.','')))=(1) AND charindex('!',[Email])<=(0) AND
charindex('#',[Email])<=(0) AND charindex('%',[Email])<=(0) AND
charindex('¤',[Email])<=(0) AND charindex('"',[Email])<=(0)))
GO

change the size of datatype in sql

I have created a table with column id as varchar2(20). Now I want to modify it and change size to 13 i.e column id varchar2(13). How to achieve it? Thanks in advance
p.s.: I don`t have any data in my table.
You may try this for Oracle:-
alter table tablename modify
(
column_name varchar2(13)
);
Also If you dont have any data in the table then you can also drop the table and then create the table with the columname as varchar2(13)
Just run this query for MySQL:
ALTER TABLE tablename CHANGE column `id` varchar2(13);
See here for more details. If you have additional constraints on this column, specify those as well.

altering a table column

create table Names (
FirstName varchar(40),
LastName varchar(40),
FullName AS FirstName+LastName
)
but now, in the full name i want that a space is inserted between first and last name, so i am altering the table but it gives syntax error. how to alter it.
alter table Names
alter column fullname as FirstName+' '+LastName
You can do this instead:
alter table Names drop column fullname
alter table Names add fullname as FirstName+' '+LastName
You can not ALTER computed columns. You can drop and recreate

Displaying the constraints in a table

Hello I am trying to display the constraints in one of my tables but for some reason I get the message no rows selected. Noted below is the table I have created.
Create table Teams (
TeamID varCHAR2(4) constraint Teams_TeamID_PK Primary Key,
TeamName VARCHAR2(40)
);
This is the code I am using to show my constraints.
SELECT constraint_name,
constraint_type,
search_condition
FROM USER_CONSTRAINTS
WHERE table_name = 'Teams';
I am a rookie so I want to make sure I understand what is wrong. I have tried to drop the table thinking that my constraints did not take - I did not, nor did I receive any errors when I created the table and I am referencing TeamID in another table. So when I try to drop the table I get an error message when is what I was hoping for.
Try this:
SELECT constraint_name,
constraint_type,
search_condition
FROM USER_CONSTRAINTS
WHERE table_name = 'TEAMS';
Unless double-quoted when created, all object names in Oracle are upper case.
I personally use:
SELECT * FROM all_constraints WHERE Table_Name = <TableName>;
Use the following code:
show create table table_name;
select dbms_mview.get_ddl('TABLE',USER,'TEAMS') from dual;
If you prefer the CamelCase names, your create table script should have been:
Create table "Teams" (
"TeamID" varCHAR2(4) constraint "Teams_TeamID_PK" Primary Key,
"TeamName" VARCHAR2(40)
);
Without double-quotes Oracle helpfully converts all identifiers to uppercase :)
Type the table name in upper case in where clause within the single quotes.
e.g. WHERE table_name = 'TEAMS';

SQL Server: Extracting a Column Into a Table

I have a table with a column that I want to extract out and put into a separate table.
For example, lets say I have a table named Contacts. Contacts has a column named Name which stores a string. Now I want to pull out the names into another table named Name and link the Contact.Name column to the Id of the Name table.
I can only use SQL to do this. Any ideas on the best way to go about this?
Let me know if I can clarify anything, thanks!
[edit]
One problem is that different contacts can be tied to the same name. So when different contacts have the same name and it gets exported the Name table would only have one unique row for that name and all the contacts would point to that row. I guess this wouldn't make sense if I were actually working on a contact book, but I'm just using it to illustrate my problem.
CREATE TABLE Name (NameID int IDENTITY(1, 1), [Name] varchar(50))
INSERT INTO Name ([Name])
SELECT DISTINCT [Name]
FROM Contact
ALTER TABLE Contact
ADD COLUMN NameID int
UPDATE Contact
SET NameID = [Name].NameID
FROM Contact
INNER JOIN [Name]
ON Contact.[Name] = [Name].[Name]
ALTER TABLE Contact
DROP COLUMN [Name]
Then add foreign key constraint, etc.
Create the new table with a Foreign key that points back to the contact table. Then insert the names and contactids from the contact table into this new table. After that you can drop the "name" column from the contact table.
CREATE TABLE Name
(
ContactId int,
Name nvarchar(100)
);
INSERT Name(Name)
SELECT ContactId, Name From Contact;
ALTER TABLE Contact
DROP Column name;
EDIT: Since you have edited the question to mention that one name can be associated with multiple contacts, this changes things in the opposite way.
CREATE TABLE Name
(
NameId int IDENTITY,
Name nvarchar(100)
);
INSERT Name(Name)
SELECT DISTINCT Name From Contact;
ALTER TABLE Contact
ADD NameId int;
UPDATE c
SET c.NameId = n.NameId
FROM Contact c
JOIN Name n on n.Name = c.Name;
ALTER Table Contact
Drop Column Name;
NOTE: Make sure that you create the appropiate foreign key between the Contact and Name tables using the NameId on the Contact table and also create a UNIQUE constraint on the "name" column in the Name table.
insert into another_table( contact_id, name )
select id, name
from contacts;
insert into new_table (contact_id, name)
select min(id), name
from contacts
group by name;
This is one way of ensuring only one row per name - you can substitute other functions for min (like, for eg max).
I'm not too sure why you would want to do this, though. No matter what, you will end up with some contacts that don't have a name linked to them...
ALTER TABLE `Contacts` ADD `name_id` INT( 12 ) NOT NULL
ALTER TABLE `Name` ADD `Name` VARCHAR( 200 ) NOT NULL
INSERT INTO Name (id, name) SELECT id, Name FROM Contacts
ALTER TABLE `Contacts` DROP `Name`
The problem is the name_id field, which is filles with "0" and should be have the same value as the id in the Contacts-Table. Here you can use the LOOP or ITERATE statement (if you using MySQL).