I have had a good look around and I, so far, haven't been able to find an answer to my question.
Bit of background;
I am new to SQL but very interested in learning. Due to moving country soon, instead of finding a job at an entry level, I am studying towards my certifications, but there are some things that books do not cover.
Now, when creating tables I understand the concept of creating a unique identifier, and I understand that there are many ways to do it, either by using;
** 'uniqueidentifier default newsequentialid()'
or
'int primary key identity' **
when creating the table.
I understand the use for each one, but my question is,
How would someone create an ID for SQL that is sequential but doesn't start at 1?
is it possible to start with 8 digits(00000001), like an employee ID for example, or are there limitations in place preventing SQL from starting anywhere other than (1)
Thanks for reading Ladies and Gents.
You guys don't mess around! thanks for the help!
You can create custom sequences using the CREATE SEQUENCE statement.
In T-SQL you can set an identity seed using the following syntax
YourID INT PRIMARY KEY IDENTITY(1000, 1)
This will start at 1000 and go up by one with each increment.
See http://msdn.microsoft.com/en-gb/library/ms186775.aspx
If you want to pad the number with zeros, I don't think you can do this with an integer type.
To start with an AUTO_INCREMENT value other than 1, you can set that value with CREATE TABLE or ALTER TABLE, like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;
using dbcc checkident, you can set your identity column to any integer value any time.
Related
How do I generate a range of numbers in one column in MySQL? I'm looking for any soluton to make numbers range that starts from 500000000 and ends on 889999999.
It seems that may want to use an AUTO_INCREMENT in the column value you want. You can set the starting value to the one you desire in this way.
Also, you can only have one AUTO_INCREMENT column in a given table.
CREATE TABLE your_table (
column_1 INT NOT NULL AUTO_INCREMENT = 500000000
--Add other columns
)
If you already have a table with the AUTO_INCREMENT column, just set the value to the one you want.
ALTER TABLE your_table AUTO_INCREMENT = 500000000;
If what you want is to insert rows with those numbers, use a loop.
Just for fun, generate the range in a text file, by any means available.
Unload to a text file.
Load that text file to your table. You don not say if you are constrained by how long this takes. It sounds like you just want a table of a single column of INT with lower and upper limits.
MySQL should just handle these numbers,this is not really a "big" range, seriously.
Do you want to constrain the values in the column to
{500000000..889999999}?
Or do you want to know how to define a column
to hold these values?
Do you want a written procedure to generate
these numbers for you?
Do you want us to size this for you?
Do you want us to write a script or program to load these?
All of these answers are available with minimal sweat. Keywords are MySQL,Integer, Types.
We cannot see your problem because your question does not describe a problem.
Tell us what you tried, and tell us what happened...
Otherwise just add them, you are still in INT territory (-2Gi..2Gi), not BIGINT yet.
Switch to MariaDB, then JOIN to a pseudo-table called seq_500000000_to_889999999.
I struggling to create a table that sets table parameters as well as creating the columns.
I am using MySQL server.
I require that the table meets the following criteria:
The table should be Called CUSTOMER with the columns CUST, LOCX, LOCY.
The column CUST will be a 1 up serial starting 1001 and will be the primary key.
LOCX and LOCY will contain X and Y Integers no greater than +-11, and will be foreign keys to other tables.
For info: I then intend to add my data to the table using the INSERT INTO function in a separate query that I already have.
Any direction on the construction of a query to create a table meeting the requirements above will be greatly appreciated
you can create a new table with a MySQL-GUI if you have problems with that.
These GUI-tools usually provide a New-Table button that also allows you to define your table without writing any code. They are often limited but should be more than sufficient for your needs. there are 1-month trial versions for paid versions and even completely free GUIs so you don't have to buy anything.
after that use the following code to retrieve "perfect" SQL from MySQL:
show create table your_schema_name.your_table_name
do that a few times and study the code. Soon you will be able to write create-table statements and include more complex column definitions on your own. It will also be easier to understand the MySQL Documentation which can be confusing and somehow intimidating with its completeness for beginners.
A bit new here, but here goes. What is the algorithm that you use to populate a Type-3 SCD? I have fact tables and dimension tables setup already, and have done this already through TOS in talend, but need the code. Using MySQL. Any kind of general idea or even reference to the right way to go about this is appreciated. Also any documentation reference is also welcome.
Well i guess this is what you are looking for.
eg we have a salesperson dimension table and we need to change its territory using SCD Type-3
Step-1
Add Column using ALTER Table Statement
ALTER TABLE SalesPersonTable ADD old_territory datatype
old_territory is added`
Step-2
Rename previous Column territory to new_territory
ALTER TABLE SalesPersonTable CHANGE territory new_territory datatype
Step-3
Update the affected SalesPerson data
UPDATE SalesPersonTable SET old_territory = new_territory WHERE SalesPersonKey=YourKeyHere
Step-4
Update the new_territory now
UPDATE SalesPersonTable SET new_territory = 'New Territory' WHERE SalesPersonKey=YourKeyHere
Now you can see here that there is nothing like algorithm here. There are just simple steps to follow. you can write your own procedures to make this. Good Luck with That.
I'm adding stuff to a MySQL table, and each item has a position that it shows up in my system like a question number.
I could find out what the largest position is before adding a new question, +1 it, and then add it - but I was wondering if there's a more intelligent way that doesn't require a second query.
Something like INSERT INTO questions (id, position) values (0, MAX(position)).
This field is not the primary key, auto_increment is of no use to this situation.
I cannot use position as the key, because the key relates to many other things, and the position can be changed at any time.
I am a pretty confident MySQL query writer, so please don't offer any suggestions other than the question asked - I know of plenty of alternatives, this is just a syntax question.
I'm sure you get my drift!
Cheers.
Use auto_increment like most other folk do?
This is the intelligent way because it concurrency safe.
Best way would be to change the field properties within MySQL, meaning let MySql do the work without providing input.
TABLE TableName CHANGE position position INT( 11 ) NOT NULL AUTO_INCREMENT
Now when inserting, do not provide any values for this field.
INSERT INTO questions (var1, var2) values (val1,val2)
MySQL will automatically increment the position value. You can set current counter as well as reset if need be. This way you are not running any quarries at all.
something like this should work
INSERT INTO questions (id, position) SELECT 0, MAX(position)+1 FROM questions;
Obviously, you will need to handle the 0 value, as in set it for something appropriate, i just copied that value off your example.
You could also create a stored procedure to use in the query, or even have a trigger which determines the MAX(position), adds one and stores it in your newly created row.
Quite alot of options there, I would go for the above solution though (with a index on position)
There's no way to set a default value to a specific calculation, it seems.
I want to add a field name called UserId datatpe varchar(25) in all the tables.If the table already contains this field I dont want to add.Is it possible by using SQL query.Please give me some Ideas.
Refer to http://www.lost-in-code.com/programming/mysql/mysql-check-if-field-exists/
It should be doable using something like:
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=’table’ AND column_name=’UserId’)
BEGIN
Alter Table....
END
You can use SHOW TABLES to get a list of all the tables in the database, and SHOW CREATE TABLE or DESCRIBE to get the specification of each table. This can then be used in application code to create the field. Exactly how to do it depends on your programming language.
I don't think it's possible using only SQL, but I'm not an SQL guru so don't take my word for it :-)