WP not giving an ID in my wp_users table - database

wp_users is creating users but without an ID in the table.

Make sure your settings for ID looks like this in MySql and ensure AUTO_INCREMENT is ticked.

Did you create wp_users manually? The ID column should be defined as auto_increment by default; extracting from one of my databases shows:
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
Try altering the column as described in this question.
Might be worth checking that the other tables have the correct structure as well (auto_increment, indices, etc). The correct definitions can be found in schema.php, here.

It is auto incremented.
No need to define when you create the users.
for the more info, please visit the wordpress.com for any help.

Related

Should I use NEWID() as my primary key in the table? What datatype I should use for that column?

I have read few posts and articles about NEWID() in MS SQL. Before I decide should I use this method or not I would like to get some information. My Single Page App has few tables. One of the tables should store unique key for each customer. I'm wondering if I should use NEWID() also how I should store that id in the table? I was looking over dataypes and there is unique-identifier type. Few articles mentioned that I will have potential problems with performance especially if I would be joining over 100k to some other tables. I will have this scenario where I would have to join these records to different tables. If anyone can provide some answers or suggestions that would be great. Thanks in advance!
Here is example of my Table:
Column Name Data Type Allow Nulls
hm_id int Unchecked // auto-increment id
hm_studentID uniqueidentifier Unchecked // primary key
hm_firstName varchar(50) Checked
hm_lastName varchar(50) Checked
hm_dob datetime Checked

Wordpress dbDelta not functioning

I'm currently writing a plugin for a customer, and while it's usually working good, I found that dbDelta does not allow me to create the table I need on plugin activation.
I'm running the below code to bind the activation function:
register_activation_hook(__FILE__, 'adminInstallation');
And this is the function itself:
function adminInstallation(){
global $wpdb;
$objectEquipment = 'wp_object_equipment';
$equipmentSQL = "CREATE TABLE ".$objectEquipment." (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL
);";
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
$equipment = dbDelta($equipmentSQL);
}
Once this has been run, I am checking the database, but no tables have been added. Trying to dump the error will only result in Wordpress telling me there was unexpected output, but it wont let me see the actual message that the server returns. This issue has been bugging me for some hours, and I cant continue until it's solved. Does anyone here have any idea why it might do this?
As far as I can tell, all the code is valid, and this is the third plugin I've written. I even tried using the code from my previous ones, but that did not work either.
EDIT: I tried running the function after the plugin activation and dump the dbDelta response. It reports that the table has been created, but still, there's nothing new in the database. Any ideas?
Thanks in advance! //
Jonathan
From http://codex.wordpress.org/Creating_Tables_with_Plugins:
You have to put each field on its own line in your SQL statement.
You have to have two spaces between the words PRIMARY KEY and the definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
You shoud echo $wpdb->last_error; after dbDelta call so you get the mysql error.
You can try this function:
$table_name = "ratings";
$table_columns = "id INT(6) UNSIGNED AUTO_INCREMENT,
rate tinyint(1) NOT NULL,
ticket_id bigint(20) NOT NULL,
response_id bigint(20) NOT NULL,
created_at TIMESTAMP";
$table_keys = "PRIMARY KEY (id),
KEY ratings_rate (rate),
UNIQUE KEY ratings_response_id (response_id)";
create_table($table_name, $table_columns, $table_keys);
I search and dbDelta is running $wpdb->query($sqlQuery); for custom queries.
For those whose dbDelta is not working use
dbDelta($sql_query_to_create_table);
Thanks!
Have a nice code
I do know that dbDelta is very particular about the formatting. I believe the issue is that you are not using backticks (`) around database parts. Try the following.
On a side note, wouldn't hurt to designate a primary key.
$equipmentSQL = "CREATE TABLE `".$objectEquipment."` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL,
PRIMARY KEY (`id`)
);";

PostgreSQL No Auto Increment function?

I have a test application coded in Java for creating an indexed and non indexed table in a MySQL, PostgreSQL, Oracle and Firebird database (Amongst other things).
Is it simply a case that PostgreSQL doesnt allow the auto increment feature? If not, what is the normal procedure for having an indexed coloumn?
Thanks in advance
You may use SERIAL in PostgreSQL to generate auto increment field,
For eg:-
CREATE TABLE user (
userid SERIAL PRIMARY KEY,
username VARCHAR(16) UNIQUE NOT NULL
)
This will create userid as auto-increment primary key indexed.
If you don't want this as primary key, just remove PRIMARY KEY.
Use a column of type SERIAL. It works the same way as AUTOINCREMEMT on some other DBs. (Check the docs for other features you can use with it.)
With current Postgres, you can just use SERIAL for the column type.
With older versions of Postgres, you can implement this using SEQUENCE; the relevant procedure is:
CREATE SEQUENCE mytable_myid_seq;
ALTER TABLE mytable ALTER COLUMN myid SET DEFAULT NEXTVAL('mytable_myid_seq');
A good article on this is MySQL versus PostgreSQL: Adding an Auto-Increment Column to a Table

how can I use GUID datatype in SQL Server 2008?

I want to build an employees table using SQL SERVER 2008 , and in my table I want to have an ID for each employee .
I heared about GUID and I kind of understood that its a data type , But I couldn't use it
could you please show me the way to use it ...
by the way , lets say I want something like this :
CREATE TABLE Employees (
ID guid PRIMARY KEY,
Name NVARCHAR(50) NOT NULL
)
How can I do it ?? because I want to benefit from it , but I couldn't find out how to do that
It's not called GUID in SQL Server. It's called uniqueidentifier
The type is called UNIQUEIDENTIFIER as others have pointed out already. But I think you absolutely must read this article before proceeding: GUIDs as PRIMARY KEYs and/or the clustering key
They are called uniqueidentifier
Don't use uniqueidentifier as primary key if clustered (which is the default for PKs)
Seriously: use a standard IDENTITY instead
You can also consider using NEWSEQUENCIALID as the default value for your ID column as it would be faster than using NEWID() generate the GUIDs.
BUT (from the same link above):-
If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID.
Practical demo, FWIW
DECLARE #guid1 AS uniqueidentifier
SET #guid1 = NEWID()
SELECT #guid1
The GUID in sql server is known by UNIQUEIDENTIFIER data type. below is the desired code.
CREATE TABLE Employees
(
Id UNIQUEIDENTIFIER PRIMARY KEY,
Name NVARCHAR (50) not null
)
GO

Database best practices

I have a table which stores comments, the comment can either come from another user, or another profile which are separate entities in this app.
My original thinking was that the table would have both user_id and profile_id fields, so if a user submits a comment, it gives the user_id leaves the profile_id blank
is this right, wrong, is there a better way?
Whatever is the best solution depends IMHO on more than just the table, but also how this is used elsewhere in the application.
Assuming that the comments are all associated with some other object, lets say you extract all the comments from that object. In your proposed design, extracting all the comments require selecting from just one table, which is efficient. But that is extracting the comments without extracting the information about the poster of each comment. Maybe you don't want to show it, or maybe they are already cached in memory.
But what if you had to retrieve information about the poster while retrieving the comments? Then you have to join with two different tables, and now the resulting record set is getting polluted with a lot of NULL values (for a profile comment, all the user fields will be NULL). The code that has to parse this result set also could get more complex.
Personally, I would probably start with the fully normalized version, and then denormalize when I start seeing performance problems
There is also a completely different possible solution to the problem, but this depends on whether or not it makes sense in the domain. What if there are other places in the application where a user and a poster can be used interchangeably? What if a User is just a special kind of a Profile? Then I think that the solution should be solved generally in the user/profile tables. For example (some abbreviated pseudo-sql):
create table AbstractProfile (ID primary key, type ) -- type can be 'user' or 'profile'
create table User(ProfileID primary key references AbstractProfile , ...)
create table Profile(ProfileID primary key references AbstractProfile , ...)
Then any place in your application, where a user or a profile can be used interchangeably, you can reference the LoginID.
If the comments are general for several objects you could create a table for each object:
user_comments (user_id, comment_id)
profile_comments (profile_id, comment_id)
Then you do not have to have any empty columns in your comments table. It will also make it easy to add new comment-source-objects in the future without touching the comments table.
Another way to solve is to always denormalize (copy) the name of the commenter on the comment and also store a reference back to the commenter via a type and an id field. That way you have a unified comments table where on you can search, sort and trim quickly. The drawback is that there isn't any real FK relationship between a comment and it's owner.
In the past I have used a centralized comments table and had a field for the fk_table it is referencing.
eg:
comments(id,fk_id,fk_table,comment_text)
That way you can use UNION queries to concatenate the data from several sources.
SELECT c.comment_text FROM comment c JOIN user u ON u.id=c.fk_id WHERE c.fk_table="user"
UNION ALL
SELECT c.comment_text FROM comment c JOIN profile p ON p.id=c.fk_id WHERE c.fk_table="profile"
This ensures that you can expand the number of objects that have comments without creating redundant tables.
Here's another approach, which allows you to maintain referential integrity through foreign keys, manage centrally, and provide the highest performance using standard database tools such as indexes and if you really need, partitioning etc:
create table actor_master_table(
type char(1) not null, /* e.g. 'u' or 'p' for user / profile */
id varchar(20) not null, /* e.g. 'someuser' or 'someprofile' */
primary key(type, id)
);
create table user(
type char(1) not null,
id varchar(20) not null,
...
check (id = 'u'),
foreign key (type, id) references actor_master_table(type, id)
);
create table profile(
type char(1) not null,
id varchar(20) not null,
...
check (id = 'p'),
foreign key (type, id) references actor_master_table(type, id)
);
create table comment(
creator_type char(1) not null,
creator_id varchar(20) not null,
comment text not null,
foreign key(creator_type, creator_id) references actor_master_table(type, id)
);

Resources