CREATE TABLE dreams (
dream_id INT PRIMARY KEY,
name VARCHAR (20),
type VARCHAR (10));
DESCRIBE dreams;
(SHOWS AN ERROR )
mysql> desc constitution;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| constitution_name | varchar(300) | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
Please see the above example.
How to DESCRIBE a TABLE in SQL
The more SQL standard confirm SQL query which uses information_schema database and this views.
And less does the same as the non standard desc MySQL's clause, which was mentioned by Pawan Tiwari answer.
Query
SELECT
information_schema.COLUMNS.COLUMN_NAME AS 'Field'
, information_schema.COLUMNS.COLUMN_TYPE AS 'Type'
, information_schema.COLUMNS.IS_NULLABLE AS 'Null'
, information_schema.COLUMNS.COLUMN_KEY AS 'Key'
, information_schema.COLUMNS.COLUMN_DEFAULT AS 'Default'
, information_schema.COLUMNS.EXTRA AS 'Extra'
FROM
information_schema.TABLES
INNER JOIN
information_schema.COLUMNS ON information_schema.TABLES.TABLE_NAME = information_schema.COLUMNS.TABLE_NAME
WHERE
information_schema.TABLES.TABLE_NAME = 'dreams'
Result
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| dream_id | int(11) | NO | PRI | | |
| name | varchar(20) | YES | | | |
| type | varchar(10) | YES | | | |
See demo
Related
I want to create a student table with column 'student_birthday' and its format should be dd-mm-yy.
create table `student`.`studentinfo`(
`student_id` int(10) not null auto_increment,
`student_name` varchar(45) not null,
`student_surname` varchar(45) not null,
`student_birthday` date(???),
(some lines of code)
primary key(student_id));
what should be inputted in the (???) to get the right format above?
Just use "DATE" without the brackets. The brackets are only needed for certain column types where you want to specify the maximum number of bytes/characters that can be stored.
For MySQL, it's documented at https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
The following example will explain your problem. I am using MySQL 5.7.18.
Firstly I have described the structure of users table as I am going to create posts table with FOREIGN KEY.
Later I created posts table and it has a DATE field named created with many other columns.
Finally I inserted 1 row in the newly created table.
mysql> desc users;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id | bigint(20) | NO | PRI | NULL | |
| fname | varchar(50) | NO | | NULL | |
| lname | varchar(50) | NO | | NULL | |
| uname | varchar(20) | NO | | NULL | |
| email | text | NO | | NULL | |
| contact | bigint(12) | NO | | NULL | |
| profile_pic | text | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> CREATE TABLE posts(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, title text NOT NULL, description text NOT NULL, posted_by bigint, FOREIGN KEY(posted_by) REFERENCES users(id) ON DELETE CASCADE ON UPDATE RESTRICT , created DATE);
Query OK, 0 rows affected (0.01 sec)
mysql> desc posts;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| title | text | NO | | NULL | |
| description | text | NO | | NULL | |
| posted_by | bigint(20) | YES | MUL | NULL | |
| created | date | YES | | NULL | |
+-------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> INSERT INTO posts(title, description, posted_by, created) values("Getting started with MySQL", "Excellent Database system", 1, "2017-05-26");
Query OK, 1 row affected (0.00 sec)
mysql> select * from posts;
+----+----------------------------+---------------------------+-----------+------------+
| id | title | description | posted_by | created |
+----+----------------------------+---------------------------+-----------+------------+
| 1 | Getting started with MySQL | Excellent Database system | 1 | 2017-05-26 |
+----+----------------------------+---------------------------+-----------+------------+
1 row in set (0.00 sec)
mysql>
The datatype date on its own is enough to represent a date value. The format will matter when you are displaying the data, for which you can use the FORMAT function on your date column.
I should add that there is a certain amount of flexibility as to the format when inserting date time literals as documented here.
Ho to export MySQL table structure as text version table?
I mean something like this:
+-----------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------+------+-----+-------------------+-----------------------------+
| EID | int(11) | NO | PRI | 0 | |
| MOD_EID | int(11) | YES | | NULL | |
| EXIT_TIME | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+-----------+------+-----+-------------------+-----------------------------+
I'm sure there is some tool which will export table like this. Does anyone know how to do this from MySQL?
you could accomplish that with 3 ways.
DESC : ease of use
SHOW CREATE TABLE : ease of create new table with another table's same schema
information_schema : difficult to use, but powerful.
1. using DESCRIBE
DESC $DB_NAME.$TBL_NAME;
sample output
mysql> DESC jsheo_test.test;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| spent | int(11) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2. using SHOW CREATE TABLE
SHOW CREATE TABLE $DB_NAME.$TBL_NAME;
sample output
mysql> SHOW CREATE TABLE jsheo_test.test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`name` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`spent` int(11) DEFAULT NULL,
`gender` char(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
3. using information schema
SELECT TABLE_NAME
, COLUMN_NAME
, ORDINAL_POSITION
, DATA_TYPE
, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '$DB_NAME'
AND TABLE_NAME = '$TBL_NAME'
ORDER BY TABLE_NAME, ORDINAL_POSITION;
sample output
+------------+-------------+------------------+-----------+-------------+
| TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | DATA_TYPE | IS_NULLABLE |
+------------+-------------+------------------+-----------+-------------+
| test | name | 1 | varchar | YES |
| test | age | 2 | int | YES |
| test | spent | 3 | int | YES |
| test | gender | 4 | char | YES |
+------------+-------------+------------------+-----------+-------------+
4. from shell
run use -e option
$ mysql -uusername -S ~/tmp/mysql.sock -e "DESC jsheo_test.test"
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| spent | int(11) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
otherwise output format is strange. something like below.
$ echo "desc jsheo_test.test;" | mysql -uusername -S /tmp/mysql.sock
Field Type Null Key Default Extra
name varchar(10) YES NULL
age int(11) YES NULL
spent int(11) YES NULL
gender char(1) YES NULL
you can SELECT some fields and store them in a OUTFILE with this command:
SELECT * FROM table_name INTO OUTFILE 'textile.txt'
the mysql command line tool allows you to do that with the command
desc tablename;
You can go for
$>SELECT * FROM TABLE_NAME
INTO OUTFILE '/PATH/TO/TEXTFILE.TXT'
OR
$>mysql -h<mysqlhostname> -u<username> -p <databasename> -e "SELECT * FROM TABLE_NAME" > myfile.txt
Replace the SQL.
I'm about ready to rip my hair out and take up poop flinging as a living!
I have a MySQL query which runs fine in MySQL
SELECT
p.ID AS DataID,
p.timestamp AS Timestamp,
sum(p.Value * v.Factor) AS Value,
v.VirtualProfiles_id AS VProfileID
FROM
profiledata p
JOIN
profilevirtualjoin v
ON
p.Profile_ID=v.Profile_ID
WHERE
v.VirtualProfiles_id = 5
GROUP BY
v.Profile_ID,
p.timestamp
But when I try to run this as a query in a SQLDataSet in Delphi
SQLDataSet2.Active := False;
SQLDataSet2.CommandText := 'SELECT p.ID AS DataID, p.timestamp AS Timestamp, sum(p.Value * v.Factor) AS Value,' +
'v.VirtualProfiles_id AS VProfileID FROM profiledata p JOIN profilevirtualjoin v ON ' +
'p.Profile_ID=v.Profile_ID WHERE v.VirtualProfiles_id = ' + InttoStr(5)
+' GROUP BY v.Profile_ID, p.timestamp';
SQLDataSet2.Active := True;
I get an error
First chance exception at $765BC41F. Exception class TDBXError with message 'Unknown column 'v.VirtualProfiles_id' in 'where clause''. Process EMVS.exe (7556)
If anyone can offer any insight, I would be most appreciative.
EDIT:
I am using the MySQL server 5.5 and Delphi XE
What I am trying to do is this:
I have a tables as follows:
Profile:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Designation | varchar(255) | YES | | NULL | |
| Description | text | YES | | NULL | |
| UnitID | int(11) | NO | PRI | NULL | |
+-------------+--------------+------+-----+---------+----------------+
profiledata
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| TimeStamp | datetime | YES | | NULL | |
| Value | double | YES | | NULL | |
| Profile_ID | int(11) | NO | PRI | NULL | |
+------------+----------+------+-----+---------+----------------+
Virtualprofiles
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| Designation | varchar(45) | NO | | NULL | |
| Description | varchar(255) | YES | | NULL | |
| Unit_ID | int(11) | NO | PRI | 0 | |
+-------------+--------------+------+-----+---------+----------------+
profilevirtualjoin
+--------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| VirtualProfiles_id | int(11) | NO | PRI | NULL | |
| Profile_ID | int(11) | NO | PRI | NULL | |
| Factor | double | NO | | 1 | |
+--------------------+---------+------+-----+---------+----------------+
What I need to do is to "produce" a new profile which is the sum of a set of existing profiles. so, the data from the profiledata table must be summed where the ProfileID is included in the virtualprofile and the timestamp values are equal.
The Problem
So, the problem is this. The DBExpress driver provided with Delphi XE can only process Dynamic SQL queries, not MySQL Queries. Although Dynamic SQL is compatible with MySQL, it is not compatible the other way around.
Quoting from the MySQL Manual (sec 12.16.3):
In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause.
MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause.
The updated DBExpress driver included with Delphi XE3 includes specific support for MySQL code, and so this limitation is not applicable.
The Workaround
The solution to this problem is to create a view in MySQL server and to call it from Delphi using only Dynamic SQL compatible code. In the end the following workaround did the trick:
In MySQL:
CREATE VIEW `VirtualProfileData` AS
SELECT
p.ID AS DataID,
p.timestamp AS Timestamp,
sum(p.Value * v.Factor) AS Value,
v.VirtualProfiles_id AS VProfileID
FROM
profiledata p
JOIN
profilevirtualjoin v
ON
p.Profile_ID=v.Profile_ID
GROUP BY
v.Profile_ID,
p.timestamp
Then in Delphi
SQLDataSet2.Active := False;
SQLDataSet2.CommandText := 'SELECT * FROM VirtualProfileData WHERE VProfileID = ' + InttoStr(5);
SQLDataSet2.Active := True;
You changed the name of the column here:
v.VirtualProfiles_id AS VProfileID
After that point, in most cases (the exception being those involving grouping or aggregation), you need to refer to the column by the new name. I think that's the case here.
Try changing your WHERE clause to use the alias instead:
WHERE v.VirtualProfiles_id = ' + InttoStr(5)
The problem is the compatibility between Mysql Types and Delphi types try to use Basics Types of delphi
In my case changing from
SELECT * FROM table_name WHERE column_name = 'VALUE';
to
SELECT * FROM table_name WHERE column_name LIKE 'VALUE';
solved the problem and return the same result.
I didn't dig dipper to figure it out what was happening, but it is a weird bug because it works fine with every other column except with a specific one.
alter table if field is not already exist
ALTER TABLE `table`
ADD( `abc` text NOT NULL,
`xyz` tinyint(1) NOT NULL,
);
if abc or xyz fields are already exist the can not be alter table
if it is possible ?
You can use a SHOW COLUMNS beforehand and construct your query accordingly, adding only fields that are missing.
Example output of SHOW COLUMNS:
mysql> SHOW COLUMNS FROM City;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
I can't comment yet, so I post answer: try this link for detailed example. It queries information_schema.COLUMNS table for column information about database tables.
I have two tabels;
mysql> describe ipinfo.ip_group_country;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ip_start | bigint(20) | NO | PRI | NULL | |
| ip_cidr | varchar(20) | NO | | NULL | |
| country_code | varchar(2) | NO | MUL | NULL | |
| country_name | varchar(64) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
mysql> describe logs.logs;
+----------------------+------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ts | timestamp | NO | | CURRENT_TIMESTAMP | |
| REMOTE_ADDR | tinytext | NO | | NULL | |
| COUNTRY_CODE | char(2) | NO | | NULL | |
+----------------------+------------+------+-----+---------------------+----------------+
I can select country code using ip address from first table:
mysql> SELECT country_code FROM ipinfo.`ip_group_country` where `ip_start` <= INET_ATON('74.125.45.100') order by ip_start desc limit 1;
+--------------+
| country_code |
+--------------+
| US |
+--------------+
In logs.logs, I have all the REMOTE_ADDR (ip address) set, but all COUNTRY_CODE entries are empty. Now, I want to populate COUNTRY_CODE appropriately using the ipinfo table. How can I do this?
thanks!
Try
UPDATE logs.logs
SET COUNTRY_CODE = (
SELECT country_code
FROM ipinfo.ip_group_country
WHERE ipinfo.ip_start <= INET_ATON(logs.REMOTE_ADDR)
LIMIT 1
)
WHERE COUNTRY_CODE IS NULL
If it fails saying the column types must match, you'll have to alter your logs.logs table so that the REMOTE_ADDR column is the same type (varchar(20)) as the ip_cidr table.
In a single-table update you use update t1 set c1=x where y.
In a multi-table update you use update t1, t2 set t1.c1=t2.c2 where t1.c3=t2.c4
Here's the relevant documentation http://dev.mysql.com/doc/refman/5.0/en/update.html
What you're looking for is something along the lines of (editted) update logs.logs as l, ipinfo.ip_group_country as c set l.COUNTRY_CODE=c.country_code where c.ip_start <= INET_ATON(l.REMOTE_ADDR) order by c.ip_start asc
Edit: you're right, the max() in the original answer I provided could not work. The query above should, although it will likely be less efficient than something like the approach in the answer provided below.