MySQL Server

MySQL Server

Explore mysql server code snippets and tutorials

MySQL Server

sql cte query with closure table

<p>The provided SQL script creates two tables, &quot;categories&quot; and &quot;childcategories,&quot; and inserts some sample data into them. It then uses a recursive common table expression (CTE) to generate a breadcrumb-like …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
CREATE TABLE categories (
    id int IDENTITY(1,1) not null,
    created_date datetime not null DEFAULT GETDATE(),
    updated_date datetime not null DEFAULT GETDATE(),
    title nvarchar(max) NOT NULL,
    CONSTRAINT PK_categories_id PRIMARY KEY CLUSTERED (id),
);


CREATE TABLE childcategories (
  parent_id int,
  child_id int,
 CONSTRAINT PK_childcategories PRIMARY KEY NONCLUSTERED ([parent_id],[child_id]),
   CONSTRAINT FK_childcategories_parent_id FOREIGN KEY (parent_id) REFERENCES  categories(id),
   CONSTRAINT FK_childcategories_child_id FOREIGN KEY (child_id) REFERENCES  categories(id),
);


insert into categories(title)
values ('hardware'), ('cpu'), ('gpu') , ('intel'), ('amd'), ('nvidia') , ('geforce')



insert into childcategories(parent_id,child_id)
values (1,2), (1,3),(2,4), (2,5),(3,4), (3,5), (3,6), (6,7)

with cte as (
		select c.id,c.title as parent_title, 1 as [level],cat.title as child_title,
		case when c.title<>cat.title then c.title + ' > ' + cat.title else c.title end as breadcrumb
		from categories as c
		left join childcategories as ch on ch.parent_id=c.id
		left join categories as cat on cat.id=ch.parent_id
		where ch.parent_id is not null
		union all
		select  cat.id,cte.parent_title as parent_title, cte.[level] + 1,cat.title as child_title,
			case when cte.parent_title<>cat.title and cte.parent_title<>par.title then cte.parent_title + ' > ' +par.title + ' > ' +  cat.title else par.title + ' > ' +cat.title end as breadcrumb
		from cte
			inner join childcategories as ch on ch.parent_id=cte.id
			inner join categories as cat on cat.id=ch.child_id
			inner join categories as par on par.id=cte.id
)

select distinct * from cte  order by id, [level];
MySQL Server

Appointments sql schema

<p>The SQL script provided creates four tables: <code>AppointmentCategories</code>, <code>Tags</code>, <code>Appointments</code>, and <code>AppointmentFiles</code>. These tables are designed to manage appointments and their related information. Below is a brief explanation of each …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Appointment  schema */

CREATE TABLE AppointmentCategories (
    Id int IDENTITY(1,1) not null,
    Title nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_AppointmentCategories_Id PRIMARY KEY CLUSTERED (Id)
);

CREATE TABLE Tags (
    Id int IDENTITY(1,1) not null,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    Title nvarchar(max) NOT NULL,
    CONSTRAINT PK_Tags_Id PRIMARY KEY CLUSTERED (Id)
);


CREATE TABLE Appointments (
    Id int IDENTITY(1,1) not null,
    AppointmentCategoryId int not null,
    Title nvarchar(max) NOT NULL,
    Description nvarchar(max) NOT NULL,
    StartDate datetime not null DEFAULT GETDATE(),
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_Appointments_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_Appointments_AppointmentCategoryId FOREIGN KEY (AppointmentCategoryId) REFERENCES  AppointmentCategories(Id)
);

CREATE TABLE AppointmentTags (
    AppointmentId int,
    TagId int,
    [Order] int not null  default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    CONSTRAINT PK_AppointmentTags PRIMARY KEY NONCLUSTERED ([ApppointmentId],[TagId]),
    CONSTRAINT FK_AppointmentTags_AppointmentId FOREIGN KEY (AppointmentId) REFERENCES  Appointments(Id),
    CONSTRAINT FK_AppointmentTags_TagId FOREIGN KEY (TagId) REFERENCES Tags(Id),
);


CREATE TABLE AppointmentFiles (
    Id int IDENTITY(1,1) not null,
    AppointmentId int not null,
    FilePath nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_AppointmentFiles_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_AppointmentFiles_AppointmentId FOREIGN KEY (AppointmentId) REFERENCES  Appointments(Id)
);

/*--------------------------------------------------------------------------------------------------------------  */
MySQL Server

events sql schema

<p>The provided SQL script creates a schema for managing events, their categories, tags, and associated media. Let&#39;s go through each table&#39;s purpose and its columns:</p> <ol> <li> <p><code>EventCategories</code> table:</p> <ul> …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Event schema */


CREATE TABLE EventCategories (
    Id int IDENTITY(1,1) not null,
    Title nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_EventCategories_Id PRIMARY KEY CLUSTERED (Id)
);

CREATE TABLE Tags (
    Id int IDENTITY(1,1) not null,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    Title nvarchar(max) NOT NULL,
    CONSTRAINT PK_Tags_Id PRIMARY KEY CLUSTERED (Id)
);


CREATE TABLE Events (
    Id int IDENTITY(1,1) not null,
    EventCategoryId int not null,
    Title nvarchar(max) NOT NULL,
    Description nvarchar(max) NOT NULL,
    StartDate datetime not null DEFAULT GETDATE(),
    EndDate datetime not null DEFAULT GETDATE(),
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_Events_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_Events_EventCategoryId FOREIGN KEY (EventCategoryId) REFERENCES  EventCategories(Id)
);

CREATE TABLE EventTags (
    EventId int,
    TagId int,
    [Order] int not null  default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    CONSTRAINT PK_EventTags PRIMARY KEY NONCLUSTERED ([EventId],[TagId]),
    CONSTRAINT FK_EventTags_EventId FOREIGN KEY (EventId) REFERENCES  Events(Id),
    CONSTRAINT FK_EventTags_TagId FOREIGN KEY (TagId) REFERENCES  Tags(Id),
);

CREATE TABLE EventMedia (
    Id int IDENTITY(1,1) not null,
    EventId int not null,
    FilePath nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_EventMedia_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_EventMedia_EventId FOREIGN KEY (EventId) REFERENCES  Events(Id)
);


/*------------------------------------------------------------------------------------------------ */
MySQL Server

blog sql schema

<p>This SQL script defines a database schema for a blogging application. It creates several tables to store information related to blog posts, categories, tags, and media associated with the posts. …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*------------------------------------------------------------------------------------------------ */

/* Blog schemna */

CREATE TABLE PostCategories (
    Id int IDENTITY(1,1) not null,
    Title nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_PostCategories_Id PRIMARY KEY CLUSTERED (Id)
);


CREATE TABLE Posts (
    Id int IDENTITY(1,1) not null,
    PostCategoryId int not null,
    Title nvarchar(max) NOT NULL,
    Description nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_Posts_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_Posts_PostCategoryId FOREIGN KEY (PostCategoryId) REFERENCES  PostCategories(Id)
);


CREATE TABLE Tags (
    Id int IDENTITY(1,1) not null,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    Title nvarchar(max) NOT NULL,
    CONSTRAINT PK_Tags_Id PRIMARY KEY CLUSTERED (Id)
);

CREATE TABLE PostTags (
    PostId int,
    TagId int,
    [Order] int not null  default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    CONSTRAINT PK_PostTags PRIMARY KEY NONCLUSTERED ([PostId],[TagId]),
    CONSTRAINT FK_PostTags_PostId FOREIGN KEY (PostId) REFERENCES  Posts(Id),
    CONSTRAINT FK_PostTags_TagId FOREIGN KEY (TagId) REFERENCES  Tags(Id),
);


CREATE TABLE PostMedia (
    Id int IDENTITY(1,1) not null,
    PostId int not null,
    FilePath nvarchar(max) NOT NULL,
    [Order] int not null default 0,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    CONSTRAINT PK_PostMedia_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_PostMedia_PostId FOREIGN KEY (PostId) REFERENCES  Posts(Id)
);

/* -----------------------------------------------------------------------------------  */