Phpminiadmin - Add new table - database

I am using phpminiadmin to access database UI of my project for live site. I want to add new table in my database but not getting any option to add new table.
Is any one knows how to add new table or create a copy of template of exiting table in phphminiadmin ?

You can create table using query as follows:
CREATE TABLE table_name (column_name column_type);
Now, we will create following table in TUTORIALS database.
tutorials_tbl(
tutorial_id INT NOT NULL AUTO_INCREMENT,
tutorial_title VARCHAR(100) NOT NULL,
tutorial_author VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( tutorial_id )
);
NOTE: table name tutorials_tbl is taken as example

You may find it under operations tab
http://prntscr.com/7cxznm

Related

On my local machine, I am unable to drop a table because it doesn't exist or I don't have permission

I am going through college and I'm doing an database class where we're introduced to the SQL Server Management Studio, so I'm very new to all of this. That being said, I've been following along and keeping up, making sure to note his queries... However, I noticed something. In the queries I've been making while keeping notes of my class, I have the error "cannot drop table because it does not exist or you don't have permissions".
Now this is odd to me, as I am the only user of this laptop, I am basically the administrator, I've created the database and tables as per instructions and yet, this issue is popping up and I'm unable to run my queries to see how they work.
Here's a snippet of my code, though I'm not sure how much it'd help...
-- Dropping tables in case they already exist
drop table Movie
drop table Genre
drop table Theater
drop table MovieTheater
-- Create table
create table Movie
(
MovieID int not null constraint PK_Movie primary key,
Title varchar(200) not null,
Budget money null,
ReleaseDate date null,
GenreCode char(1) not null constraint FK_MovieToGenre references Genre(GenreCode),
Released bit not null,
MovieLength decimal(5,2) null
)
create table Genre
(
GenreCode char(1) not null constraint PK_Genre primary key,
GenreDescription varchar(30) not null
)
create table Theater
(
TheaterID int not null constraint PK_Theater primary key,
TheaterName varchar(100) not null,
Address varchar(50) not null,
City varchar(50) not null,
Province char(2) not null,
PostalCode char(7) not null,
PhoneNumber char(13) not null
)
create table MovieTheater
(
MovieID int not null,
TheaterID int not null,
StartDate date not null,
EndDate date null,
constraint PK_MovieTheater primary key (MovieID, TheaterID)
)
I attempted changing the permissions of the database but it wouldn't allow me. Other solutions I've looked up all assume that it's connecting to a database for other purposes (likely work related)
The error is pretty clear. cannot drop table because it does not exist. You can't DROP a table that doesn't exist, just as you can't CREATE a table that already exists.
In SQL Server 2016 and later you can use DROP TABLE IF EXISTS to drop a table if it exists. Since the oldest version in mainstream support is SQL Server 2019, you can reasonably expect that IF EXISTS will work on any new database
DROP TABLE IF EXISTS [dbo].[MyTable0];
In older, now unsupported, versions you had to check explicitly in an IF :
IF OBJECT_ID(N'dbo.MyTable0', N'U') IS NOT NULL
DROP TABLE [dbo].[MyTable0];
The SQL Server DROP TABLE IF EXISTS Examples shows all these options. Use DROP TABLE IF EXISTS unless you really need to work with an unsupported database version.
From your code, I think the problem is you tried to drop the tables before you created them
-- Dropping tables in case they already exist
drop table Movie
drop table Genre
drop table Theater
drop table MovieTheater
To drop the table if only it has already exist, you could try:
IF OBJECT_ID('tableName', 'U') IS NOT NULL
DROP TABLE tableName;
Explain:
The OBJECT_ID function returns the object ID of the specified table. If the table does not exist, it returns NULL. The 'U' parameter indicates that we are looking for a user-defined table.
The IF statement checks whether the table exists by checking the result of the OBJECT_ID function. Simple as that!

Why does PgAdmin 4 (v 6.1) not allow me to Add Data in the GUI>

I am using PgAdmin4 v 6.1 on Mac OSX Monterrey 12.1 and I cannot add data in the GUI. Why can i not add the data?
I created my first table in the Gui And defined 3 columns
I cannot add a row in the GUI using Rt Click <edit First 100> The Return query is all read only and the columns have lock icons?
here is the table create sql:
- Table: public.products
-- DROP TABLE IF EXISTS public.products;
CREATE TABLE IF NOT EXISTS public.products
(
name character varying COLLATE pg_catalog."default" NOT NULL,
price integer NOT NULL,
id integer NOT NULL DEFAULT nextval('products_id_seq'::regclass)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.products
OWNER to postgres;
The UI path to get to the locked edit screen:
here is the image of the screen with lock icons:
I was able to write insert SQL and add a row.
But I still cannot edit that row in the GUI.
I made one Column the Primary Key and I was able to Edit + Add Data
Here is the new SQL for Table Create:
CREATE TABLE IF NOT EXISTS public.products
(
name character varying COLLATE pg_catalog."default" NOT NULL,
price integer NOT NULL,
id integer NOT NULL DEFAULT nextval('products_id_seq'::regclass),
CONSTRAINT products_pkey PRIMARY KEY (id)
)

Cannot insert explicit value for identity column

I am migrating my application form one database to other with keeping table structure as it is. I am creating same tables in new table and inserted value using db link.
I am getting error message like "Cannot insert explicit value for identity column in table 'XYZ' when IDENTITY_INSERT is set to OFF." because table XYZ have ScreenConfigSettingAccessId as an identity column
Below is the script I am using for creating table and inserting value
CREATE TABLE [dbo].[XYZ](
[ScreenConfigSettingAccessId] [int] IDENTITY(1,1) NOT NULL,
[APP_ID] [int] NOT NULL,
[ScreenConfigSettingId] [int] NOT NULL,
[RSRC_ID] [char](20) NOT NULL)
)
INSERT INTO [dbo].[XYX]
(
[ScreenConfigSettingAccessId] ,
[APP_ID] ,
[ScreenConfigSettingId] ,
[RSRC_ID]
)
SELECT
[ScreenConfigSettingAccessId] ,
[APP_ID] ,
[ScreenConfigSettingId] ,
[RSRC_ID]
FROM [olddatabase].[database name].[dbo].[XYX]
in old table the value of ScreenConfigSettingAccessId is 3 and 4.
I want to inset the same data which old table have so set IDENTITY_INSERT to ON and tried but it still not allowing to insert.
Looking for you suggestions
You need to specify the table. Check out the command syntax in SQL Books Online: SQL 2000 or SQL 2012 (the syntax hasn't changed).

Auto increment primary key in SQL Server Management Studio 2012

How do I auto increment the primary key in a SQL Server database table? I've had a look through the forum but can't see how to do this.
I've looked at the properties but can't see an option. I saw an answer where you go to the Identity specification property and set it to yes and set the Identity increment to 1, but that section is grayed out and I can't change the no to yes.
There must be a simple way to do this but I can't find it.
Make sure that the Key column's datatype is int and then setting identity manually, as image shows
Or just run this code
-- ID is the name of the [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)
the code will run, if ID is not the only column in the table
image reference fifo's
When you're creating the table, you can create an IDENTITY column as follows:
CREATE TABLE (
ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
...
);
The IDENTITY property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an ALTER TABLE command.
Edit:
Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables. I guess if you want to make a column an identity column, you HAVE to use an ALTER TABLE command.
You have to expand the Identity section to expose increment and seed.
Edit: I assumed that you'd have an integer datatype, not char(10). Which is reasonable I'd say and valid when I posted this answer
Expand your database, expand your table right click on your table and select design from dropdown.
Now go Column properties below of it scroll down and find Identity Specification, expand it and you will find Is Identity make it Yes. Now choose Identity Increment right below of it give the value you want to increment in it.
CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.
In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.
Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will NOT have to specify a value for the "Personid" column (a unique value will be added automatically):
Perhaps I'm missing something but why doesn't this work with the SEQUENCE object? Is this not what you're looking for?
Example:
CREATE SCHEMA blah.
GO
CREATE SEQUENCE blah.blahsequence
START WITH 1
INCREMENT BY 1
NO CYCLE;
CREATE TABLE blah.de_blah_blah
(numbers bigint PRIMARY KEY NOT NULL
......etc
When referencing the squence in say an INSERT command just use:
NEXT VALUE FOR blah.blahsequence
More information and options for SEQUENCE
When you're using Data Type: int you can select the row which you want to get autoincremented and go to the column properties tag. There you can set the identity to 'yes'. The starting value for autoincrement can also be edited there. Hope I could help ;)
I had this issue where I had already created the table and could not change it without dropping the table so what I did was:
(Not sure when they implemented this but had it in SQL 2016)
Right click on the table in the Object Explorer:
Script Table as > DROP And CREATE To > New Query Editor Window
Then do the edit to the script said by Josien; scroll to the bottom where the CREATE TABLE is, find your Primary Key and append IDENTITY(1,1) to the end before the comma. Run script.
The DROP and CREATE script was also helpful for me because of this issue. (Which the generated script handles.)
You can use the keyword IDENTITY as the data type to the column along with PRIMARY KEY constraint when creating the table.
ex:
StudentNumber IDENTITY(1,1) PRIMARY KEY
In here the first '1' means the starting value and the second '1' is the incrementing value.
If the table is already populated it is not possible to change a column to IDENTITY column or convert it to non IDENTITY column. You would need to export all the data out then you can change column type to IDENTITY or vice versa and then import data back.
I know it is painful process but I believe there is no alternative except for using sequence as mentioned in this post.
Be carefull like if you want the ID elements to be contigius or not. As SQLSERVER ID can jump by 1000 .
Examle: before restart ID=11
after restart , you insert new row in the table, then the id will be 1012.
You could do the following: New Table Creation:
-- create new table with Column ID which is Primary Key and Auto Increment --
CREATE TABLE titles(
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, --Primary Key with Auto-Increment --
keyword VARCHAR(260),
status VARCHAR(10),
);
If you Table Already exists and need to make the changes to ID column to be auto-increment and Primary key, then see below:
ALTER TABLE table DROP COLUMN id; // drop the existing ID in the table
ALTER TABLE table ADD id int IDENTITY(1, 1) NOT NULL; // add new column ID with auto-increment
ALTER TABLE table ADD CONSTRAINT PK_ident_test PRIMARY KEY CLUSTERED (id); // make it primary key

Mysql auto increment alternatives

I have the below table.
create table mytable(
physical_id int auto_increment,
logical_id int,
data varchar(20),
version_start_date datetime,
version_end_date datetime,
primary key(physical_id),
unique key(logical_id,version_start_date, version_end_date)
);
The idea behind the schema is, I want to keep track of modification to
every row and find the valid row on any particular date by checking the
version_start_date and version_end_date. I want my logical id to be
auto_increment, but mysql allows only one id to be auto_increment.
So, I want to set logical_id to physical_id, when creating a new row. I
am able to do it using the trigger.
delimiter $$
create trigger myTrigger before insert on mytable for each row begin set
new.logical_id = (select auto_increment from information_schema.tables
where table_schema = database() and table_name = 'mytable') ; end$$
delimiter ;
Few other options I checked out are, http://feedblog.org/2007/06/20/portable-sequence-generation-with-mysql/ and http://www.redhat.com/docs/en-US/JBoss_Hibernate/3.2.4.sp01.cp03/html/Reference_Guide/Native_SQL-Custom_SQL_for_create_update_and_delete.html
The problem with these approaches is, I have to create a new sequence table and keep inserting a record into that table.
Is there a better alternative?
Thank you
Bala
-- Update
I am not sure why #tpdi, why I need parent, when I can just emulate this with below table.
create table logical_id_seq (
logical_id int auto_increment,
primary key(logical_id)
);
create table mytable (
physical_id int auto_increment,
logical_id int not null references parent(logical_id),
data varchar(20),
version_start_date datetime not null,
version_end_date datetime not null,
primary key(physical_id),
foreign key (logical_id) references logical_id_seq(logical_id),
unique key (logical_id,version_start_date,version_end_date)
);
This is why I prefer Oracle/PostgreSQL sequences to MySQL's auto_increment and SQL Server's IDENTITY - you'd be able to define multiple sequences for this.
Creating a separate table solely for a column in order to use auto_increment for an additional auto_increment is the best solution I can think of, accessed via trigger or stored procedure.
Why wouldn't you want to have a table with the current version, and then an additional table for a history? Then, you use an insert into the current version table to generate your ID, and only query the history table when you actually need the history information.
Now I understand your issue :). Changing my answer.
You will need to use some trigger so that whenever some row is edited logical id is incremented by 1 finding max of logical id available in table.
create table parent (
logical_id int auto_increment,
physical_id int null references mytable(id)
);
create table mytable(
physical_id int auto_increment,
logical_id not null references parent(logical_id),
data varchar(20),
version_start_date datetime,
version_end_date datetime,
primary key(physical_id)
);
To insert a new instance of an existing record, insert into mytable ...., then capture new mytable.id, and update parent's physical_id with it (possibly in a trigger).
Now, to look up the current record, look up the logical id in parent, and use parent.physical_id to join to the correct record in "mytable"
Parent points to the current valid record (of all records) in "mytable".
To find all instances of a logical record, use the logical_id in "mytable".
To insert a wholly new record, first insert into parent to get the new logical_id, then insert the data in "mytable"; this is why we allow parent.physical_id to be nullable.
As to the OP's update: yes, you can "emulate" by just "stealing" a sequence, but that doesn't model what's really going on. "Stealing" the sequence is just an implementation detail; having two tables as I've shown above makes explicit what's really going on, namely: I have a current state (which parent points to) in "mytable", and 0, 1 or many prior states in "mytable". It's a cleaner separation of concerns, a table that tells you "what's current for any id", and a table that tells you "the data of that current state, and the data of prior states".

Resources