USE asterisk_db;

SHOW DATABASES;

SHOW TABLES;


SELECT * FROM `ps_endpoints`;

DELETE FROM `ps_endpoints` WHERE `id` = '1004';

SELECT * FROM `extensions_table`;
DESCRIBE `extensions_table`;

ALTER TABLE `extensions_table`
ADD CONSTRAINT `extension_table_unique_val_two_check` UNIQUE (`exten`, `appdata`);


ALTER TABLE `extensions_table`
DROP INDEX `extension_table_unique_val`;


ALTER TABLE `extensions_table`
ADD CONSTRAINT `extension_table_unique_val` UNIQUE (`exten`, `appdata`, `priority`);

DELETE FROM `extensions_table` WHERE priority = 4 and exten = "1005" ;
DELETE FROM `extensions_table` WHERE exten = "1005" OR exten = "1004";

DELETE FROM `ps_endpoints` WHERE id = "1004" OR id = "1005";
DELETE FROM `extensions_table` WHERE exten = "1004" OR exten = "1005";

DELETE FROM `ps_auths` WHERE id = "1004" OR id = "1005";
DELETE FROM `ps_aors` WHERE id = "1004" OR id = "1005";

SELECT * FROM `extensions_table` WHERE app = "Dial";
SELECT * FROM extensions_table WHERE context = 'default' AND exten = '1005';
SELECT * FROM extensions_table WHERE exten = '1004' AND context = 'default';



SELECT * FROM `ps_endpoints`;
SELECT * FROM `extensions_table`;
-- 
SELECT * FROM ps_auths;
SELECT * FROM ps_aors;
DESCRIBE `ps_auths`;
DESCRIBE `ps_aors`;
DESCRIBE `extensions_table`;

INSERT INTO `ps_aors`(`id`,`max_contacts`) VALUES ("1004", 1);
INSERT INTO `ps_auths`(`id`, `auth_type`, `username`, `password`, `md5_cred`, `realm`) VALUES("1004", "userpass", "1004", "12345", null, null);




-- USER ROLES ROLE GOES HERE 
CREATE TABLE `roles`(
`role_id` INTEGER NOT NULL AUTO_INCREMENT,
`role_name` VARCHAR(20) UNIQUE NOT NULL,
CONSTRAINT `roles_pk` PRIMARY KEY (`role_id`)
)ENGINE = 'Innodb' AUTO_INCREMENT = 1, DEFAULT CHARSET 'latin1';

DESCRIBE `roles`;


CREATE TABLE `user_roles`(
`u_id` INTEGER NOT NULL,
`role_id` INTEGER NOT NULL,
CONSTRAINT `user_roles_pk` PRIMARY KEY(`u_id`,`role_id`)
)ENGINE = 'Innodb' AUTO_INCREMENT = 1, DEFAULT CHARSET 'latin1';


-- foreign key to be added to this table in alter table form  
CREATE TABLE `user`(
`u_id` INTEGER NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(70) UNIQUE NOT NULL,
`password` VARCHAR(68) NOT NULL,
-- fk to uder_account 
CONSTRAINT `user_table_pk` PRIMARY KEY(`u_id`)
)ENGINE = 'Innodb', AUTO_INCREMENT = 1, DEFAULT CHARSET 'latin1';

ALTER TABLE `user` ADD COLUMN `user_email_id` VARCHAR(50) UNIQUE NOT NULL; 


ALTER TABLE `user_roles` ADD CONSTRAINT `user_lones_U_fk_to_user` FOREIGN KEY(`u_id`) REFERENCES `user`(`u_id`);
ALTER TABLE `user_roles` ADD CONSTRAINT `user_lones_R_fk_to_user` FOREIGN KEY(`role_id`) REFERENCES `roles`(`role_id`);
DESC `user_roles`;


INSERT `roles`(`role_name`) VALUES ('ROLE_admin');

SELECT * FROM `user`;
SELECT * FROM `user_roles`;
SELECT * FROM `roles`;

DELETE FROM `user` WHERE `user_name` = 'Mathew Francis';
DELETE FROM `user_roles` WHERE `u_id` = (SELECT `u_id` FROM `user_roles` LIMIT 1);
DELETE FROM `roles` WHERE `roles`.`role_name` = 'ROLE_admin'




