Found 6702 Articles for Database

Generating a range of numbers in MySQL?

Jennifer Nicholas
Updated on 25-Jun-2020 11:21:38

2K+ Views

To generate a range of numbers in MySQL, you can use stored procedure. Firstly, we need to create a table. After that, we will create a stored procedure that generates a range of number from 10 to 1.The following is the query to create a table −mysql> create table GeneratingNumbersDemo    −> (       −> Number int    −> ); Query OK, 0 rows affected (0.55 sec)Now you can create a stored procedure that stores a range of numbers in the table. The following is the query to create a stored procedure −mysql> delimiter // mysql> CREATE PROCEDURE ... Read More

How add unique key to existing table (with non-unique rows)?

Anvi Jain
Updated on 25-Jun-2020 11:23:55

642 Views

You can add unique key to existing table with the help of alter command. The syntax is as follows −ALTER TABLE yourTableName ADD CONSTRAINT yourConstraintName UNIQUE(yourColumnName1, yourColumnName2, ............N);To understand the above concept, let us create a table with some columns. The query to create a table −mysql> create table MovieCollectionDemo    −> (       −> MovieId int,       −> MovieDirectorName varchar(200),       −> NumberOfSongs int unsigned    −> ); Query OK, 0 rows affected (0.62 sec)Now you can check the table does not have any unique constraint. The query to check unique constraint is ... Read More

How to change Table Engine in MySQL?

Vrundesha Joshi
Updated on 29-Jun-2020 07:37:52

274 Views

You can change table engine with the help of alter command. The syntax is as follows −alter table yourTableName ENGINE = yourEngineName;To understand the above syntax let us create a table with engine MyISAM. Later you can change any other engine. The following is the query to create a table.mysql> create table ChangeEngineTableDemo −> (    −> MovieId int,    −> MovieName varchar(100),    −> IsPopular bool −> )ENGINE = 'MyISAM'; Query OK, 0 rows affected (0.37 sec)Look at the above query, the table engine is MyISAM, now you can change it to any other engine. Here, we will change ... Read More

Get the week number in MySQL for day of week?

Anvi Jain
Updated on 25-Jun-2020 11:25:15

167 Views

MySQL DAYOFWEEK() function returns 1 for Sunday, 2 for Monday and so on for day of week. Let us see an example by first creating a table −mysql> create table DayOfWeekDemo −> (    −> Issuedate datetime −> ); Query OK, 0 rows affected (0.52 sec)Inserting date in the table with the help of insert command. The query is as follows −mysql> insert into DayOfWeekDemo values(date_add(curdate(), interval 5 day)); Query OK, 1 row affected (0.52 sec) mysql> insert into DayOfWeekDemo values(date_add(curdate(), interval 6 day)); Query OK, 1 row affected (0.13 sec) mysql> insert into DayOfWeekDemo values(date_add(curdate(), interval 7 ... Read More

How to insert date in single quotes with MySQL date formats?

Vrundesha Joshi
Updated on 25-Jun-2020 11:25:58

598 Views

To insert the date with date formats, use the str_to_date() function with date in single quotes. The following is the syntax −insert into yourTableName values(Value1, value2, ......ValueN, str_to_date(‘anyDate’, ’%Y-%m-%d’));Here are the Date Formats in MySQL −FormatDescription%aAbbreviated weekday name (Sun to Sat)%bAbbreviated month name (Jan to Dec)%cNumeric month name (0 to 12)%DDay of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, ...)%dDay of the month as a numeric value (01 to 31)%eDay of the month as a numeric value (0 to 31)%fMicroseconds (000000 to 999999)%HHour (00 to 23)%hHour (00 to 12)%IHour (00 to 12)%iMinutes (00 to 59)%jDay ... Read More

Delete all records from a table in MySQL?

Rishi Rathor
Updated on 29-Jun-2020 07:20:56

14K+ Views

To delete all the records from a table in MySQL, use the TRUNCATE command. Let us fir see the syntax −TRUNCATE TABLE yourTableName.The above syntax will delete all the records from a table. Let us create a table to understand the above syntax −mysql> create table TruncateTableDemo −> (    −> BookId int    −> ,    −> BookName varchar(200) −> ); Query OK, 0 rows affected (0.54 sec)Inserting records in the table with the help of insert command. The query to insert records in the table are as follows −mysql> insert into TruncateTableDemo values(1001, 'C in Dept'); Query OK, ... Read More

Create a new table with the properties of an old table and without duplicates using MySQL LIKE Operator?

Jennifer Nicholas
Updated on 29-Jun-2020 07:22:13

43 Views

To achieve this with LIKE operator, the following is the syntax −CREATE TABLE yourTableName2 LIKE yourTableName1;To understand the syntax, let us create a table and insert some records into it. The following is the query to create a table −mysql> create table Employee −> (    −> EmployeeId int    −> ,    −> EmployeeName varchar(100) −> ); Query OK, 0 rows affected (0.54 sec)Inserting records into the table with the help of insert command. The query is as follows −mysql> insert into Employee values(1, 'Carol'); Query OK, 1 row affected (0.18 sec) mysql> insert into Employee values(2, 'John'); ... Read More

Move rows from one table to another in MySQL?

Anvi Jain
Updated on 29-Jun-2020 07:23:06

3K+ Views

You can move rows from one table to another with the help of INSERT INTO SELECT statement.The syntax is as follows −insert into yourDestinationTableName select *from yourOriginalTable where someConditionTo understand the above syntax. let us create a table. The following is the query to create a table −mysql> create table StudentTable    −> (       −> Id int,       −> Name varchar(100)    −> ); Query OK, 0 rows affected (0.65 sec)Now, I will create second table. The query is as follows −mysql> create table Employee    −> (       −> EmployeeId int   ... Read More

MySQL extract year from date format?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

296 Views

To extract year from date format, you can use in-built function YEAR() from MySQL. The query is as follows −mysql> SELECT YEAR(curdate()) as OnlyYearFromCurrentDate;The following is the output −+-------------------------+ | OnlyYearFromCurrentDate | +-------------------------+ | 2018 | +-------------------------+ 1 row in set (0.00 sec)Or you can do in the following way −mysql> SELECT YEAR(date(now())) as OnlyYearFromCurrentDate;The following is the output −+-------------------------+ | OnlyYearFromCurrentDate | +-------------------------+ | 2018 ... Read More

Set existing column as Primary Key in MySQL?

Jennifer Nicholas
Updated on 29-Jun-2020 07:24:28

10K+ Views

You can set primary key on an existing column in MySQL with the help of alter command.The syntax is as follows to add primary key to an existing column.ALTER TABLE yourTableName ADD PRIMARY KEY(yourColumnName);To set existing column as primary key, let us first create a table. The query to create a table −mysql> create table AddingPrimaryKeyDemo    −> (       −> UniversityId int,       −> UniversityName varchar(200)    −> ); Query OK, 0 rows affected (1.16 sec)Look at the above query, I haven’t added primary key. Let us check the same with the help of DESC ... Read More

Advertisements