Remove Primary Key from MySQL Table

AmitDiwan
Updated on 22-Aug-2019 06:31:37

481 Views

Yes, we can remove a primary key using the DROP in MySQL. Following is the syntax −alter table yourTableName drop primary key;Let us first create a table −mysql> create table DemoTable    (       UserId int NOT NULL PRIMARY KEY    ); Query OK, 0 rows affected (0.58 sec)Following is the query to check the description of table −mysql> desc DemoTable;This will produce the following output displaying the Primary Key −+--------+---------+------+-----+---------+-------+ | Field  | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | UserId | int(11) | NO | PRI ... Read More

Print Corner Elements and Their Sum in a 2D Matrix in C Program

Sunidhi Bansal
Updated on 22-Aug-2019 06:29:35

3K+ Views

Given an array of size 2X2 and the challenge is to print the sum of all the corner elements stored in an array.Assume a matrix mat[r][c], with some row “r” and column “c” starting row and column from 0, then its corner elements will be; mat[0][0], mat[0][c-1], mat[r-1][0], mat[r-1][c-1]. Now the task is to get these corner elements and sum those corner elements i.e., mat[0][0] + mat[0][c-1] + mat[r-1][0] + mat[r-1][c-1], and print the result on the screen.ExampleInput: Enter the matrix elements :    10 2 10    2 3 4    10 4 10 Output: sum of matrix is ... Read More

Print Balanced Bracket Expression Using Given Brackets in C

Sunidhi Bansal
Updated on 22-Aug-2019 06:24:14

376 Views

Given four variables a, b, c, d with predefined values that will print the given bracket depending upon the variable used.Where variable, a for (( b for () c for )( d for ))The task is to use all the given brackets and print the balanced bracket expression, if we cannot form a balanced bracket expression then print -1. In case of multiple answers we can print any of the multiple answers which can be formed using the given brackets.ExampleInput: a = 3, b = 2, c = 4, d = 3 Output : (((((()()()()())))))()()To achieve this result we can, ... Read More

Print Last Occurrence of Elements in Array in C Program

Sunidhi Bansal
Updated on 22-Aug-2019 06:07:00

332 Views

Given an array a[] with elements and the task is to print the last occurrences of the given elements in the list. Here we not only have to remove the duplicate elements but also we have to maintain the order of the occurrences of the elements in an array as per the last time they have occurred.Like we have an array of 6 elements also containing some duplicate values i.e., {1, 3, 2, 3, 1, 2} so the result should be in form of 3 1 2.ExampleInput: a[]={4, 2, 2, 4, 1, 5, 1} Output : 2 4 5 1AlgorithmSTART ... Read More

Print Reverse of a Linked List Without Extra Space in C

Sunidhi Bansal
Updated on 22-Aug-2019 05:57:17

476 Views

The task is to print the nodes starting from the end of the linked list without using extra space which means there shouldn’t be any extra variable instead the head pointer pointing the first node will be moved.ExampleInput: 10 21 33 42 89 Output: 89 42 33 21 10There can be many solutions to print the linked list in reverse order, like recursive approach (use extra space), reverse the linked list(requires modification in the given Linked List), pushing the elements on a stack and then pop and display the elements one by one(requires space O(n)), but these solutions seem to ... Read More

Get Max ID of Row Data in MySQL

AmitDiwan
Updated on 21-Aug-2019 12:16:55

7K+ Views

To get max(id), use MAX() method in MySQL. Following is the syntax −select MAX(yourColumnName) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable710 (Id int); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable710 values(1001); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(2001); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable710 values(1998); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(1789); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable710 values(1678); Query OK, 1 ... Read More

Execute MySQL Query from the Terminal Without Printing Results

AmitDiwan
Updated on 21-Aug-2019 12:15:23

607 Views

Let us first create a table −mysql> create table DemoTable709 (Amount int); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable709 values(100); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable709 values(560); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable709 values(7800); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable709 values(1020); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable709;This will produce the following output -+--------+ | Amount | +--------+ | 100 ... Read More

Display Records After a Particular Date in MySQL

AmitDiwan
Updated on 21-Aug-2019 12:12:22

2K+ Views

Let us first create a table −mysql> create table DemoTable708 (    CustomerName varchar(100),    ShippingDate date ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable708 values('John', '2019-01-21'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable708 values('Chris', '2019-03-24'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable708 values('Robert', '2019-04-26'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable708 values('David', '2019-07-22'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable708;This will produce the ... Read More

MySQL Query to Order By NULL Values

AmitDiwan
Updated on 21-Aug-2019 12:10:19

205 Views

Let us first create a table −mysql> create table DemoTable707 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(100),    StudentMarks int ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable707(StudentFirstName, StudentMarks) values('John', 45); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable707(StudentFirstName, StudentMarks) values(NULL, 65); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable707(StudentFirstName, StudentMarks) values('Chris', 78); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable707(StudentFirstName, StudentMarks) values(NULL, 89); Query OK, 1 row affected (0.19 sec) mysql> insert into ... Read More

MySQL Query to Get Current Date Records

AmitDiwan
Updated on 21-Aug-2019 12:07:06

233 Views

To achieve this, following is the syntax wherein we have used DATE(NOW()) −select *from yourTableName where DATE(yourColumnName)=DATE(NOW());Let us first create a table −mysql> create table DemoTable706 (    UserId varchar(100),    UserName varchar(100),    UserSignupDate datetime ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable706 values('John1@gmail.com', 'John', '2019-01-31 12:45:22'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable706 values('Chris123@gmail.com', 'Chris', '2019-07-22 10:05:02'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable706 values('12Robert@gmail.com', 'Robert', '2019-06-22 11:25:22'); Query OK, 1 row affected (0.22 sec) mysql> insert into ... Read More

Advertisements